<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 2 章 核心语法 | Python 编程</title>
  <meta name="description" content="使用Python进行编程" />
  <meta name="generator" content="bookdown 0.17.2 and GitBook 2.6.7" />

  <meta property="og:title" content="第 2 章 核心语法 | Python 编程" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="使用Python进行编程" />
  <meta name="github-repo" content="yangjh-xbmu/learningpython" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 2 章 核心语法 | Python 编程" />
  
  <meta name="twitter:description" content="使用Python进行编程" />
  

<meta name="author" content="杨志宏" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="ide.html"/>
<link rel="next" href="oop.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />











<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css\style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">LearningPython</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Python 简介</a><ul>
<li class="chapter" data-level="0.1" data-path="index.html"><a href="index.html#python-发展历史"><i class="fa fa-check"></i><b>0.1</b> Python 发展历史</a></li>
<li class="chapter" data-level="0.2" data-path="index.html"><a href="index.html#python-特点"><i class="fa fa-check"></i><b>0.2</b> Python 特点</a></li>
<li class="chapter" data-level="0.3" data-path="index.html"><a href="index.html#使用-python-的知名项目"><i class="fa fa-check"></i><b>0.3</b> 使用 Python 的知名项目</a></li>
<li class="chapter" data-level="0.4" data-path="index.html"><a href="index.html#学习资源"><i class="fa fa-check"></i><b>0.4</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="ide.html"><a href="ide.html"><i class="fa fa-check"></i><b>1</b> Python 开发环境搭建</a><ul>
<li class="chapter" data-level="1.1" data-path="ide.html"><a href="ide.html#在-windows-中安装-python-3"><i class="fa fa-check"></i><b>1.1</b> 在 Windows 中安装 Python 3</a></li>
<li class="chapter" data-level="1.2" data-path="ide.html"><a href="ide.html#在-macos-中安装-python-3"><i class="fa fa-check"></i><b>1.2</b> 在 MacOS 中安装 Python 3</a><ul>
<li class="chapter" data-level="1.2.1" data-path="ide.html"><a href="ide.html#安装-xcode"><i class="fa fa-check"></i><b>1.2.1</b> 安装 Xcode</a></li>
<li class="chapter" data-level="1.2.2" data-path="ide.html"><a href="ide.html#安装-homebrew"><i class="fa fa-check"></i><b>1.2.2</b> 安装 Homebrew</a></li>
<li class="chapter" data-level="1.2.3" data-path="ide.html"><a href="ide.html#安装-python3"><i class="fa fa-check"></i><b>1.2.3</b> 安装 Python3</a></li>
<li class="chapter" data-level="1.2.4" data-path="ide.html"><a href="ide.html#使用-python3"><i class="fa fa-check"></i><b>1.2.4</b> 使用 Python3</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="ide.html"><a href="ide.html#使用-python-虚拟环境"><i class="fa fa-check"></i><b>1.3</b> 使用 python 虚拟环境</a><ul>
<li class="chapter" data-level="1.3.1" data-path="ide.html"><a href="ide.html#为什么要用虚拟环境"><i class="fa fa-check"></i><b>1.3.1</b> 为什么要用虚拟环境</a></li>
<li class="chapter" data-level="1.3.2" data-path="ide.html"><a href="ide.html#虚拟环境的创建与使用"><i class="fa fa-check"></i><b>1.3.2</b> 虚拟环境的创建与使用</a></li>
<li class="chapter" data-level="1.3.3" data-path="ide.html"><a href="ide.html#使用-pipenv-管理虚拟环境"><i class="fa fa-check"></i><b>1.3.3</b> 使用 pipenv 管理虚拟环境</a></li>
<li class="chapter" data-level="1.3.4" data-path="ide.html"><a href="ide.html#虚拟环境与系统环境的区别"><i class="fa fa-check"></i><b>1.3.4</b> 虚拟环境与系统环境的区别</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="ide.html"><a href="ide.html#编辑器"><i class="fa fa-check"></i><b>1.4</b> 编辑器</a><ul>
<li class="chapter" data-level="1.4.1" data-path="ide.html"><a href="ide.html#visual-studio-code-中的必要设置"><i class="fa fa-check"></i><b>1.4.1</b> Visual Studio Code 中的必要设置</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>I 核心语法</b></span></li>
<li class="chapter" data-level="2" data-path="systax.html"><a href="systax.html"><i class="fa fa-check"></i><b>2</b> 核心语法</a><ul>
<li class="chapter" data-level="2.1" data-path="systax.html"><a href="systax.html#注释"><i class="fa fa-check"></i><b>2.1</b> 注释</a></li>
<li class="chapter" data-level="2.2" data-path="systax.html"><a href="systax.html#变量"><i class="fa fa-check"></i><b>2.2</b> 变量</a><ul>
<li class="chapter" data-level="2.2.1" data-path="systax.html"><a href="systax.html#变量名称"><i class="fa fa-check"></i><b>2.2.1</b> 变量名称</a></li>
<li class="chapter" data-level="2.2.2" data-path="systax.html"><a href="systax.html#变量赋值"><i class="fa fa-check"></i><b>2.2.2</b> 变量赋值</a></li>
<li class="chapter" data-level="2.2.3" data-path="systax.html"><a href="systax.html#同步赋值"><i class="fa fa-check"></i><b>2.2.3</b> 同步赋值</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="systax.html"><a href="systax.html#数字类型"><i class="fa fa-check"></i><b>2.3</b> 数字类型</a><ul>
<li class="chapter" data-level="2.3.1" data-path="systax.html"><a href="systax.html#查看变量类型"><i class="fa fa-check"></i><b>2.3.1</b> 查看变量类型</a></li>
<li class="chapter" data-level="2.3.2" data-path="systax.html"><a href="systax.html#整型"><i class="fa fa-check"></i><b>2.3.2</b> 整型</a></li>
<li class="chapter" data-level="2.3.3" data-path="systax.html"><a href="systax.html#浮点类型"><i class="fa fa-check"></i><b>2.3.3</b> 浮点类型</a></li>
<li class="chapter" data-level="2.3.4" data-path="systax.html"><a href="systax.html#复数"><i class="fa fa-check"></i><b>2.3.4</b> 复数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="systax.html"><a href="systax.html#运算符"><i class="fa fa-check"></i><b>2.4</b> 运算符</a><ul>
<li class="chapter" data-level="2.4.1" data-path="systax.html"><a href="systax.html#运算符的优先级别"><i class="fa fa-check"></i><b>2.4.1</b> 运算符的优先级别</a></li>
<li class="chapter" data-level="2.4.2" data-path="systax.html"><a href="systax.html#增强赋值运算符"><i class="fa fa-check"></i><b>2.4.2</b> 增强赋值运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="systax.html"><a href="systax.html#序列"><i class="fa fa-check"></i><b>2.5</b> 序列</a><ul>
<li class="chapter" data-level="2.5.1" data-path="systax.html"><a href="systax.html#索引"><i class="fa fa-check"></i><b>2.5.1</b> 索引</a></li>
<li class="chapter" data-level="2.5.2" data-path="systax.html"><a href="systax.html#分片"><i class="fa fa-check"></i><b>2.5.2</b> 分片</a></li>
<li class="chapter" data-level="2.5.3" data-path="systax.html"><a href="systax.html#序列相加"><i class="fa fa-check"></i><b>2.5.3</b> 序列相加</a></li>
<li class="chapter" data-level="2.5.4" data-path="systax.html"><a href="systax.html#序列相乘"><i class="fa fa-check"></i><b>2.5.4</b> 序列相乘</a></li>
<li class="chapter" data-level="2.5.5" data-path="systax.html"><a href="systax.html#成员资格"><i class="fa fa-check"></i><b>2.5.5</b> 成员资格</a></li>
<li class="chapter" data-level="2.5.6" data-path="systax.html"><a href="systax.html#长度最小值最大值"><i class="fa fa-check"></i><b>2.5.6</b> 长度、最小值、最大值</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="systax.html"><a href="systax.html#字符串"><i class="fa fa-check"></i><b>2.6</b> 字符串</a><ul>
<li class="chapter" data-level="2.6.1" data-path="systax.html"><a href="systax.html#创建字符串"><i class="fa fa-check"></i><b>2.6.1</b> 创建字符串</a></li>
<li class="chapter" data-level="2.6.2" data-path="systax.html"><a href="systax.html#字符串的不可变性"><i class="fa fa-check"></i><b>2.6.2</b> 字符串的不可变性</a></li>
<li class="chapter" data-level="2.6.3" data-path="systax.html"><a href="systax.html#字符串操作"><i class="fa fa-check"></i><b>2.6.3</b> 字符串操作</a></li>
<li class="chapter" data-level="2.6.4" data-path="systax.html"><a href="systax.html#字符串分片"><i class="fa fa-check"></i><b>2.6.4</b> 字符串分片</a></li>
<li class="chapter" data-level="2.6.5" data-path="systax.html"><a href="systax.html#in-和-not-in-操作符"><i class="fa fa-check"></i><b>2.6.5</b> in 和 not in 操作符</a></li>
<li class="chapter" data-level="2.6.6" data-path="systax.html"><a href="systax.html#string-对象的方法"><i class="fa fa-check"></i><b>2.6.6</b> String 对象的方法</a></li>
<li class="chapter" data-level="2.6.7" data-path="systax.html"><a href="systax.html#比较字符串"><i class="fa fa-check"></i><b>2.6.7</b> 比较字符串</a></li>
<li class="chapter" data-level="2.6.8" data-path="systax.html"><a href="systax.html#遍历字符串"><i class="fa fa-check"></i><b>2.6.8</b> 遍历字符串</a></li>
<li class="chapter" data-level="2.6.9" data-path="systax.html"><a href="systax.html#字符串内容检验"><i class="fa fa-check"></i><b>2.6.9</b> 字符串内容检验</a></li>
<li class="chapter" data-level="2.6.10" data-path="systax.html"><a href="systax.html#在字符串内查找和替换"><i class="fa fa-check"></i><b>2.6.10</b> 在字符串内查找和替换</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="systax.html"><a href="systax.html#列表"><i class="fa fa-check"></i><b>2.7</b> 列表</a><ul>
<li class="chapter" data-level="2.7.1" data-path="systax.html"><a href="systax.html#列表赋值"><i class="fa fa-check"></i><b>2.7.1</b> 列表赋值</a></li>
<li class="chapter" data-level="2.7.2" data-path="systax.html"><a href="systax.html#删除元素"><i class="fa fa-check"></i><b>2.7.2</b> 删除元素</a></li>
<li class="chapter" data-level="2.7.3" data-path="systax.html"><a href="systax.html#分片赋值"><i class="fa fa-check"></i><b>2.7.3</b> 分片赋值</a></li>
<li class="chapter" data-level="2.7.4" data-path="systax.html"><a href="systax.html#列表对象常用内置方法"><i class="fa fa-check"></i><b>2.7.4</b> 列表对象常用内置方法</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="systax.html"><a href="systax.html#字典"><i class="fa fa-check"></i><b>2.8</b> 字典</a><ul>
<li class="chapter" data-level="2.8.1" data-path="systax.html"><a href="systax.html#创建字典"><i class="fa fa-check"></i><b>2.8.1</b> 创建字典</a></li>
<li class="chapter" data-level="2.8.2" data-path="systax.html"><a href="systax.html#获取修改和添加字典元素"><i class="fa fa-check"></i><b>2.8.2</b> 获取、修改和添加字典元素</a></li>
<li class="chapter" data-level="2.8.3" data-path="systax.html"><a href="systax.html#遍历字典"><i class="fa fa-check"></i><b>2.8.3</b> 遍历字典</a></li>
<li class="chapter" data-level="2.8.4" data-path="systax.html"><a href="systax.html#字典常用方法"><i class="fa fa-check"></i><b>2.8.4</b> 字典常用方法</a></li>
<li class="chapter" data-level="2.8.5" data-path="systax.html"><a href="systax.html#字典的排序"><i class="fa fa-check"></i><b>2.8.5</b> 字典的排序</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="systax.html"><a href="systax.html#元组"><i class="fa fa-check"></i><b>2.9</b> 元组</a><ul>
<li class="chapter" data-level="2.9.1" data-path="systax.html"><a href="systax.html#创建元组"><i class="fa fa-check"></i><b>2.9.1</b> 创建元组</a></li>
<li class="chapter" data-level="2.9.2" data-path="systax.html"><a href="systax.html#元组相关方法"><i class="fa fa-check"></i><b>2.9.2</b> 元组相关方法</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="systax.html"><a href="systax.html#控制声明"><i class="fa fa-check"></i><b>2.10</b> 控制声明</a><ul>
<li class="chapter" data-level="2.10.1" data-path="systax.html"><a href="systax.html#分支判断"><i class="fa fa-check"></i><b>2.10.1</b> 分支判断</a></li>
<li class="chapter" data-level="2.10.2" data-path="systax.html"><a href="systax.html#分支嵌套"><i class="fa fa-check"></i><b>2.10.2</b> 分支嵌套</a></li>
<li class="chapter" data-level="2.10.3" data-path="systax.html"><a href="systax.html#三元运算符"><i class="fa fa-check"></i><b>2.10.3</b> 三元运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.11" data-path="systax.html"><a href="systax.html#循环"><i class="fa fa-check"></i><b>2.11</b> 循环</a><ul>
<li class="chapter" data-level="2.11.1" data-path="systax.html"><a href="systax.html#for-循环"><i class="fa fa-check"></i><b>2.11.1</b> for 循环</a></li>
<li class="chapter" data-level="2.11.2" data-path="systax.html"><a href="systax.html#范围循环"><i class="fa fa-check"></i><b>2.11.2</b> 范围循环</a></li>
<li class="chapter" data-level="2.11.3" data-path="systax.html"><a href="systax.html#while-循环"><i class="fa fa-check"></i><b>2.11.3</b> while 循环</a></li>
<li class="chapter" data-level="2.11.4" data-path="systax.html"><a href="systax.html#中断循环"><i class="fa fa-check"></i><b>2.11.4</b> 中断循环</a></li>
<li class="chapter" data-level="2.11.5" data-path="systax.html"><a href="systax.html#继续循环"><i class="fa fa-check"></i><b>2.11.5</b> 继续循环</a></li>
</ul></li>
<li class="chapter" data-level="2.12" data-path="systax.html"><a href="systax.html#函数"><i class="fa fa-check"></i><b>2.12</b> 函数</a><ul>
<li class="chapter" data-level="2.12.1" data-path="systax.html"><a href="systax.html#创建函数"><i class="fa fa-check"></i><b>2.12.1</b> 创建函数</a></li>
<li class="chapter" data-level="2.12.2" data-path="systax.html"><a href="systax.html#函数返回值"><i class="fa fa-check"></i><b>2.12.2</b> 函数返回值</a></li>
<li class="chapter" data-level="2.12.3" data-path="systax.html"><a href="systax.html#全局变量和局域变量"><i class="fa fa-check"></i><b>2.12.3</b> 全局变量和局域变量</a></li>
<li class="chapter" data-level="2.12.4" data-path="systax.html"><a href="systax.html#参数的默认值"><i class="fa fa-check"></i><b>2.12.4</b> 参数的默认值</a></li>
<li class="chapter" data-level="2.12.5" data-path="systax.html"><a href="systax.html#关键字参数"><i class="fa fa-check"></i><b>2.12.5</b> 关键字参数</a></li>
<li class="chapter" data-level="2.12.6" data-path="systax.html"><a href="systax.html#返回多个值"><i class="fa fa-check"></i><b>2.12.6</b> 返回多个值</a></li>
<li class="chapter" data-level="2.12.7" data-path="systax.html"><a href="systax.html#函数文档字符串"><i class="fa fa-check"></i><b>2.12.7</b> 函数文档字符串</a></li>
<li class="chapter" data-level="2.12.8" data-path="systax.html"><a href="systax.html#lambda表达式"><i class="fa fa-check"></i><b>2.12.8</b> lambda表达式</a></li>
<li class="chapter" data-level="2.12.9" data-path="systax.html"><a href="systax.html#args-和-kwargs"><i class="fa fa-check"></i><b>2.12.9</b> <code>*args</code> 和 <code>**kwargs</code></a></li>
<li class="chapter" data-level="2.12.10" data-path="systax.html"><a href="systax.html#参考资料"><i class="fa fa-check"></i><b>2.12.10</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="oop.html"><a href="oop.html"><i class="fa fa-check"></i><b>3</b> Python中的面向对象编程</a><ul>
<li class="chapter" data-level="3.1" data-path="oop.html"><a href="oop.html#python对象和类"><i class="fa fa-check"></i><b>3.1</b> Python对象和类</a><ul>
<li class="chapter" data-level="3.1.1" data-path="oop.html"><a href="oop.html#创建类"><i class="fa fa-check"></i><b>3.1.1</b> 创建类</a></li>
<li class="chapter" data-level="3.1.2" data-path="oop.html"><a href="oop.html#从类中创建对象"><i class="fa fa-check"></i><b>3.1.2</b> 从类中创建对象</a></li>
<li class="chapter" data-level="3.1.3" data-path="oop.html"><a href="oop.html#隐藏数据字段"><i class="fa fa-check"></i><b>3.1.3</b> 隐藏数据字段</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="oop.html"><a href="oop.html#操作符重载"><i class="fa fa-check"></i><b>3.2</b> 操作符重载</a></li>
<li class="chapter" data-level="3.3" data-path="oop.html"><a href="oop.html#继承和多态"><i class="fa fa-check"></i><b>3.3</b> 继承和多态</a><ul>
<li class="chapter" data-level="3.3.1" data-path="oop.html"><a href="oop.html#多重继承"><i class="fa fa-check"></i><b>3.3.1</b> 多重继承</a></li>
<li class="chapter" data-level="3.3.2" data-path="oop.html"><a href="oop.html#重写方法"><i class="fa fa-check"></i><b>3.3.2</b> 重写方法</a></li>
<li class="chapter" data-level="3.3.3" data-path="oop.html"><a href="oop.html#判断对象是否属于某类"><i class="fa fa-check"></i><b>3.3.3</b> 判断对象是否属于某类</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="exception.html"><a href="exception.html"><i class="fa fa-check"></i><b>4</b> 异常处理</a><ul>
<li class="chapter" data-level="4.1" data-path="exception.html"><a href="exception.html#捕获异常"><i class="fa fa-check"></i><b>4.1</b> 捕获异常</a><ul>
<li class="chapter" data-level="4.1.1" data-path="exception.html"><a href="exception.html#try-except"><i class="fa fa-check"></i><b>4.1.1</b> try-except</a></li>
<li class="chapter" data-level="4.1.2" data-path="exception.html"><a href="exception.html#多个except"><i class="fa fa-check"></i><b>4.1.2</b> 多个except</a></li>
<li class="chapter" data-level="4.1.3" data-path="exception.html"><a href="exception.html#自定义异常"><i class="fa fa-check"></i><b>4.1.3</b> 自定义异常</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="decorator.html"><a href="decorator.html"><i class="fa fa-check"></i><b>5</b> Python 装饰器</a><ul>
<li class="chapter" data-level="5.1" data-path="decorator.html"><a href="decorator.html#为什么要用装饰器"><i class="fa fa-check"></i><b>5.1</b> 为什么要用装饰器</a></li>
<li class="chapter" data-level="5.2" data-path="decorator.html"><a href="decorator.html#简单装饰器"><i class="fa fa-check"></i><b>5.2</b> 简单装饰器</a></li>
<li class="chapter" data-level="5.3" data-path="decorator.html"><a href="decorator.html#语法糖"><i class="fa fa-check"></i><b>5.3</b> @ 语法糖</a></li>
<li class="chapter" data-level="5.4" data-path="decorator.html"><a href="decorator.html#argskwargs"><i class="fa fa-check"></i><b>5.4</b> <code>*args</code>、<code>**kwargs</code></a></li>
<li class="chapter" data-level="5.5" data-path="decorator.html"><a href="decorator.html#带参数的装饰器"><i class="fa fa-check"></i><b>5.5</b> 带参数的装饰器</a></li>
<li class="chapter" data-level="5.6" data-path="decorator.html"><a href="decorator.html#类装饰器"><i class="fa fa-check"></i><b>5.6</b> 类装饰器</a></li>
<li class="chapter" data-level="5.7" data-path="decorator.html"><a href="decorator.html#functools.wraps"><i class="fa fa-check"></i><b>5.7</b> functools.wraps</a></li>
<li class="chapter" data-level="5.8" data-path="decorator.html"><a href="decorator.html#装饰器顺序"><i class="fa fa-check"></i><b>5.8</b> 装饰器顺序</a></li>
<li class="chapter" data-level="" data-path="decorator.html"><a href="decorator.html#参考资料-1"><i class="fa fa-check"></i>参考资料</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="module.html"><a href="module.html"><i class="fa fa-check"></i><b>6</b> 模块</a><ul>
<li class="chapter" data-level="6.1" data-path="module.html"><a href="module.html#创建模块"><i class="fa fa-check"></i><b>6.1</b> 创建模块</a></li>
<li class="chapter" data-level="6.2" data-path="module.html"><a href="module.html#使用模块中的指定内容"><i class="fa fa-check"></i><b>6.2</b> 使用模块中的指定内容</a></li>
<li class="chapter" data-level="6.3" data-path="module.html"><a href="module.html#dir函数"><i class="fa fa-check"></i><b>6.3</b> dir函数</a></li>
<li class="chapter" data-level="6.4" data-path="module.html"><a href="module.html#包"><i class="fa fa-check"></i><b>6.4</b> 包</a></li>
</ul></li>
<li class="part"><span><b>II 进阶</b></span></li>
<li class="chapter" data-level="7" data-path="buildinfunctions.html"><a href="buildinfunctions.html"><i class="fa fa-check"></i><b>7</b> 内置函数</a><ul>
<li class="chapter" data-level="7.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#dict函数"><i class="fa fa-check"></i><b>7.1</b> dict函数</a><ul>
<li class="chapter" data-level="7.1.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述"><i class="fa fa-check"></i><b>7.1.1</b> 描述</a></li>
<li class="chapter" data-level="7.1.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法"><i class="fa fa-check"></i><b>7.1.2</b> 语法</a></li>
<li class="chapter" data-level="7.1.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值"><i class="fa fa-check"></i><b>7.1.3</b> 返回值</a></li>
<li class="chapter" data-level="7.1.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例"><i class="fa fa-check"></i><b>7.1.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#zip函数"><i class="fa fa-check"></i><b>7.2</b> zip函数</a><ul>
<li class="chapter" data-level="7.2.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-1"><i class="fa fa-check"></i><b>7.2.1</b> 描述</a></li>
<li class="chapter" data-level="7.2.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-1"><i class="fa fa-check"></i><b>7.2.2</b> 语法</a></li>
<li class="chapter" data-level="7.2.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-1"><i class="fa fa-check"></i><b>7.2.3</b> 返回值</a></li>
<li class="chapter" data-level="7.2.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#特殊用法"><i class="fa fa-check"></i><b>7.2.4</b> 特殊用法</a></li>
<li class="chapter" data-level="7.2.5" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-1"><i class="fa fa-check"></i><b>7.2.5</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#list函数"><i class="fa fa-check"></i><b>7.3</b> list函数</a><ul>
<li class="chapter" data-level="7.3.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-2"><i class="fa fa-check"></i><b>7.3.1</b> 描述</a></li>
<li class="chapter" data-level="7.3.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-2"><i class="fa fa-check"></i><b>7.3.2</b> 语法</a></li>
<li class="chapter" data-level="7.3.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-2"><i class="fa fa-check"></i><b>7.3.3</b> 返回值</a></li>
<li class="chapter" data-level="7.3.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-2"><i class="fa fa-check"></i><b>7.3.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#min函数"><i class="fa fa-check"></i><b>7.4</b> min函数</a><ul>
<li class="chapter" data-level="7.4.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-3"><i class="fa fa-check"></i><b>7.4.1</b> 描述</a></li>
<li class="chapter" data-level="7.4.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-3"><i class="fa fa-check"></i><b>7.4.2</b> 语法</a></li>
<li class="chapter" data-level="7.4.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-3"><i class="fa fa-check"></i><b>7.4.3</b> 返回值</a></li>
<li class="chapter" data-level="7.4.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-3"><i class="fa fa-check"></i><b>7.4.4</b> 实例</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="iterator.html"><a href="iterator.html"><i class="fa fa-check"></i><b>8</b> Python可迭代对象</a><ul>
<li class="chapter" data-level="8.1" data-path="iterator.html"><a href="iterator.html#可迭代对象"><i class="fa fa-check"></i><b>8.1</b> 可迭代对象</a></li>
<li class="chapter" data-level="8.2" data-path="iterator.html"><a href="iterator.html#迭代器"><i class="fa fa-check"></i><b>8.2</b> 迭代器</a></li>
<li class="chapter" data-level="8.3" data-path="iterator.html"><a href="iterator.html#生成器"><i class="fa fa-check"></i><b>8.3</b> 生成器</a></li>
<li class="chapter" data-level="8.4" data-path="iterator.html"><a href="iterator.html#三者之间关系"><i class="fa fa-check"></i><b>8.4</b> 三者之间关系</a></li>
<li class="chapter" data-level="8.5" data-path="iterator.html"><a href="iterator.html#迭代器长度的计算"><i class="fa fa-check"></i><b>8.5</b> 迭代器长度的计算</a></li>
<li class="chapter" data-level="8.6" data-path="iterator.html"><a href="iterator.html#yield"><i class="fa fa-check"></i><b>8.6</b> yield</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="underline.html"><a href="underline.html"><i class="fa fa-check"></i><b>9</b> 单、双下划线的区别</a><ul>
<li class="chapter" data-level="9.1" data-path="underline.html"><a href="underline.html#单下划线开头"><i class="fa fa-check"></i><b>9.1</b> 单下划线开头</a></li>
<li class="chapter" data-level="9.2" data-path="underline.html"><a href="underline.html#双下划线开头"><i class="fa fa-check"></i><b>9.2</b> 双下划线开头</a></li>
<li class="chapter" data-level="9.3" data-path="underline.html"><a href="underline.html#双下划线开头和结尾"><i class="fa fa-check"></i><b>9.3</b> 双下划线开头和结尾</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="re.html"><a href="re.html"><i class="fa fa-check"></i><b>10</b> 正则表达式</a><ul>
<li class="chapter" data-level="10.1" data-path="re.html"><a href="re.html#正则表达式发展简史"><i class="fa fa-check"></i><b>10.1</b> 正则表达式发展简史</a></li>
<li class="chapter" data-level="10.2" data-path="re.html"><a href="re.html#python中使用正则表达式的流程"><i class="fa fa-check"></i><b>10.2</b> Python中使用正则表达式的流程</a></li>
<li class="chapter" data-level="10.3" data-path="re.html"><a href="re.html#正则表达式的构成要素"><i class="fa fa-check"></i><b>10.3</b> 正则表达式的构成要素</a><ul>
<li class="chapter" data-level="10.3.1" data-path="re.html"><a href="re.html#定界符"><i class="fa fa-check"></i><b>10.3.1</b> 定界符</a></li>
<li class="chapter" data-level="10.3.2" data-path="re.html"><a href="re.html#原子"><i class="fa fa-check"></i><b>10.3.2</b> 原子</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="re.html"><a href="re.html#元字符"><i class="fa fa-check"></i><b>10.4</b> 元字符</a><ul>
<li class="chapter" data-level="10.4.1" data-path="re.html"><a href="re.html#匹配单个字符的元字符"><i class="fa fa-check"></i><b>10.4.1</b> 匹配单个字符的元字符</a></li>
<li class="chapter" data-level="10.4.2" data-path="re.html"><a href="re.html#常用转义符"><i class="fa fa-check"></i><b>10.4.2</b> 常用转义符</a></li>
<li class="chapter" data-level="10.4.3" data-path="re.html"><a href="re.html#提供计数功能的元字符"><i class="fa fa-check"></i><b>10.4.3</b> 提供计数功能的元字符</a></li>
<li class="chapter" data-level="10.4.4" data-path="re.html"><a href="re.html#匹配位置的元字符"><i class="fa fa-check"></i><b>10.4.4</b> 匹配位置的元字符</a></li>
<li class="chapter" data-level="10.4.5" data-path="re.html"><a href="re.html#其他元字符"><i class="fa fa-check"></i><b>10.4.5</b> 其他元字符</a></li>
</ul></li>
<li class="chapter" data-level="10.5" data-path="re.html"><a href="re.html#模式修饰符"><i class="fa fa-check"></i><b>10.5</b> 模式修饰符</a></li>
<li class="chapter" data-level="10.6" data-path="re.html"><a href="re.html#在python中使用正则表达式"><i class="fa fa-check"></i><b>10.6</b> 在Python中使用正则表达式</a><ul>
<li class="chapter" data-level="10.6.1" data-path="re.html"><a href="re.html#search"><i class="fa fa-check"></i><b>10.6.1</b> search()</a></li>
<li class="chapter" data-level="10.6.2" data-path="re.html"><a href="re.html#compile"><i class="fa fa-check"></i><b>10.6.2</b> compile()</a></li>
<li class="chapter" data-level="10.6.3" data-path="re.html"><a href="re.html#findall"><i class="fa fa-check"></i><b>10.6.3</b> findall()</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="argparse.html"><a href="argparse.html"><i class="fa fa-check"></i><b>11</b> 命令行参数模块argparse</a><ul>
<li class="chapter" data-level="11.1" data-path="argparse.html"><a href="argparse.html#创建解析器"><i class="fa fa-check"></i><b>11.1</b> 创建解析器</a></li>
<li class="chapter" data-level="11.2" data-path="argparse.html"><a href="argparse.html#添加参数选项"><i class="fa fa-check"></i><b>11.2</b> 添加参数选项</a><ul>
<li class="chapter" data-level="11.2.1" data-path="argparse.html"><a href="argparse.html#name-or-flags"><i class="fa fa-check"></i><b>11.2.1</b> name or flags</a></li>
<li class="chapter" data-level="11.2.2" data-path="argparse.html"><a href="argparse.html#help"><i class="fa fa-check"></i><b>11.2.2</b> help</a></li>
<li class="chapter" data-level="11.2.3" data-path="argparse.html"><a href="argparse.html#default和type"><i class="fa fa-check"></i><b>11.2.3</b> default和type</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="argparse.html"><a href="argparse.html#参数解析"><i class="fa fa-check"></i><b>11.3</b> 参数解析</a></li>
<li class="chapter" data-level="11.4" data-path="argparse.html"><a href="argparse.html#小结"><i class="fa fa-check"></i><b>11.4</b> 小结</a></li>
<li class="chapter" data-level="11.5" data-path="argparse.html"><a href="argparse.html#参考文献"><i class="fa fa-check"></i><b>11.5</b> 参考文献</a></li>
</ul></li>
<li class="part"><span><b>III 数据抓取</b></span></li>
<li class="chapter" data-level="12" data-path="spider.html"><a href="spider.html"><i class="fa fa-check"></i><b>12</b> 网络爬虫</a><ul>
<li class="chapter" data-level="12.1" data-path="spider.html"><a href="spider.html#网络爬虫原理"><i class="fa fa-check"></i><b>12.1</b> 网络爬虫原理</a></li>
<li class="chapter" data-level="12.2" data-path="spider.html"><a href="spider.html#http请求原理"><i class="fa fa-check"></i><b>12.2</b> HTTP请求原理</a><ul>
<li class="chapter" data-level="12.2.1" data-path="spider.html"><a href="spider.html#url"><i class="fa fa-check"></i><b>12.2.1</b> URL</a></li>
<li class="chapter" data-level="12.2.2" data-path="spider.html"><a href="spider.html#请求方式"><i class="fa fa-check"></i><b>12.2.2</b> 请求方式</a></li>
<li class="chapter" data-level="12.2.3" data-path="spider.html"><a href="spider.html#requests包"><i class="fa fa-check"></i><b>12.2.3</b> requests包</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="spider.html"><a href="spider.html#编码"><i class="fa fa-check"></i><b>12.3</b> 编码</a><ul>
<li class="chapter" data-level="12.3.1" data-path="spider.html"><a href="spider.html#编码方式"><i class="fa fa-check"></i><b>12.3.1</b> 编码方式</a></li>
<li class="chapter" data-level="12.3.2" data-path="spider.html"><a href="spider.html#编码转换"><i class="fa fa-check"></i><b>12.3.2</b> 编码转换</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="spider.html"><a href="spider.html#存储"><i class="fa fa-check"></i><b>12.4</b> 存储</a><ul>
<li class="chapter" data-level="12.4.1" data-path="spider.html"><a href="spider.html#存储到文件"><i class="fa fa-check"></i><b>12.4.1</b> 存储到文件</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="13" data-path="httplib.html"><a href="httplib.html"><i class="fa fa-check"></i><b>13</b> HTTP库</a><ul>
<li class="chapter" data-level="13.1" data-path="httplib.html"><a href="httplib.html#urllib"><i class="fa fa-check"></i><b>13.1</b> urllib</a><ul>
<li class="chapter" data-level="13.1.1" data-path="httplib.html"><a href="httplib.html#发送请求"><i class="fa fa-check"></i><b>13.1.1</b> 发送请求</a></li>
<li class="chapter" data-level="13.1.2" data-path="httplib.html"><a href="httplib.html#处理异常"><i class="fa fa-check"></i><b>13.1.2</b> 处理异常</a></li>
<li class="chapter" data-level="13.1.3" data-path="httplib.html"><a href="httplib.html#解析链接"><i class="fa fa-check"></i><b>13.1.3</b> 解析链接</a></li>
</ul></li>
<li class="chapter" data-level="13.2" data-path="httplib.html"><a href="httplib.html#使用requests"><i class="fa fa-check"></i><b>13.2</b> 使用requests</a><ul>
<li class="chapter" data-level="13.2.1" data-path="httplib.html"><a href="httplib.html#安装requests"><i class="fa fa-check"></i><b>13.2.1</b> 安装requests</a></li>
<li class="chapter" data-level="13.2.2" data-path="httplib.html"><a href="httplib.html#发起请求"><i class="fa fa-check"></i><b>13.2.2</b> 发起请求</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="httplib.html"><a href="httplib.html#网页编码检测及转换"><i class="fa fa-check"></i><b>13.3</b> 网页编码检测及转换</a></li>
<li class="chapter" data-level="13.4" data-path="httplib.html"><a href="httplib.html#参考资料-2"><i class="fa fa-check"></i><b>13.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="pyquery.html"><a href="pyquery.html"><i class="fa fa-check"></i><b>14</b> Pyquery</a><ul>
<li class="chapter" data-level="14.1" data-path="pyquery.html"><a href="pyquery.html#安装"><i class="fa fa-check"></i><b>14.1</b> 安装</a></li>
<li class="chapter" data-level="14.2" data-path="pyquery.html"><a href="pyquery.html#初始化"><i class="fa fa-check"></i><b>14.2</b> 初始化</a></li>
<li class="chapter" data-level="14.3" data-path="pyquery.html"><a href="pyquery.html#获取信息"><i class="fa fa-check"></i><b>14.3</b> 获取信息</a><ul>
<li class="chapter" data-level="14.3.1" data-path="pyquery.html"><a href="pyquery.html#通过选择符选定元素"><i class="fa fa-check"></i><b>14.3.1</b> 通过选择符选定元素</a></li>
<li class="chapter" data-level="14.3.2" data-path="pyquery.html"><a href="pyquery.html#通过迭代获取最终结果"><i class="fa fa-check"></i><b>14.3.2</b> 通过迭代获取最终结果</a></li>
</ul></li>
<li class="chapter" data-level="14.4" data-path="pyquery.html"><a href="pyquery.html#参考资料-3"><i class="fa fa-check"></i><b>14.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="15" data-path="databases.html"><a href="databases.html"><i class="fa fa-check"></i><b>15</b> 数据存储</a><ul>
<li class="chapter" data-level="15.1" data-path="databases.html"><a href="databases.html#mysql的存储"><i class="fa fa-check"></i><b>15.1</b> MySQL的存储</a><ul>
<li class="chapter" data-level="15.1.1" data-path="databases.html"><a href="databases.html#安装pymysql和mysql"><i class="fa fa-check"></i><b>15.1.1</b> 安装PyMySQL和MySQL</a></li>
<li class="chapter" data-level="15.1.2" data-path="databases.html"><a href="databases.html#连接数据库"><i class="fa fa-check"></i><b>15.1.2</b> 连接数据库</a></li>
<li class="chapter" data-level="15.1.3" data-path="databases.html"><a href="databases.html#对数据库进行操作"><i class="fa fa-check"></i><b>15.1.3</b> 对数据库进行操作</a></li>
</ul></li>
<li class="chapter" data-level="15.2" data-path="databases.html"><a href="databases.html#mongodb"><i class="fa fa-check"></i><b>15.2</b> MongoDB</a><ul>
<li class="chapter" data-level="15.2.1" data-path="databases.html"><a href="databases.html#在macos中的安装"><i class="fa fa-check"></i><b>15.2.1</b> 在macOS中的安装</a></li>
<li class="chapter" data-level="15.2.2" data-path="databases.html"><a href="databases.html#连接mongodb"><i class="fa fa-check"></i><b>15.2.2</b> 连接MongoDB</a></li>
<li class="chapter" data-level="15.2.3" data-path="databases.html"><a href="databases.html#指定数据库"><i class="fa fa-check"></i><b>15.2.3</b> 指定数据库</a></li>
<li class="chapter" data-level="15.2.4" data-path="databases.html"><a href="databases.html#指定集合"><i class="fa fa-check"></i><b>15.2.4</b> 指定集合</a></li>
<li class="chapter" data-level="15.2.5" data-path="databases.html"><a href="databases.html#插入数据"><i class="fa fa-check"></i><b>15.2.5</b> 插入数据</a></li>
<li class="chapter" data-level="15.2.6" data-path="databases.html"><a href="databases.html#查询数据-1"><i class="fa fa-check"></i><b>15.2.6</b> 查询数据</a></li>
<li class="chapter" data-level="15.2.7" data-path="databases.html"><a href="databases.html#计数-1"><i class="fa fa-check"></i><b>15.2.7</b> 计数</a></li>
<li class="chapter" data-level="15.2.8" data-path="databases.html"><a href="databases.html#排序"><i class="fa fa-check"></i><b>15.2.8</b> 排序</a></li>
<li class="chapter" data-level="15.2.9" data-path="databases.html"><a href="databases.html#偏移和限定"><i class="fa fa-check"></i><b>15.2.9</b> 偏移和限定</a></li>
<li class="chapter" data-level="15.2.10" data-path="databases.html"><a href="databases.html#更新"><i class="fa fa-check"></i><b>15.2.10</b> 更新</a></li>
<li class="chapter" data-level="15.2.11" data-path="databases.html"><a href="databases.html#删除"><i class="fa fa-check"></i><b>15.2.11</b> 删除</a></li>
<li class="chapter" data-level="15.2.12" data-path="databases.html"><a href="databases.html#其他操作"><i class="fa fa-check"></i><b>15.2.12</b> 其他操作</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="databases.html"><a href="databases.html#参考资料-4"><i class="fa fa-check"></i><b>15.3</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="selenium.html"><a href="selenium.html"><i class="fa fa-check"></i><b>16</b> 使用Selenium抓取动态渲染页面</a><ul>
<li class="chapter" data-level="16.1" data-path="selenium.html"><a href="selenium.html#安装-1"><i class="fa fa-check"></i><b>16.1</b> 安装</a></li>
<li class="chapter" data-level="16.2" data-path="selenium.html"><a href="selenium.html#selenium的使用"><i class="fa fa-check"></i><b>16.2</b> Selenium的使用</a><ul>
<li class="chapter" data-level="16.2.1" data-path="selenium.html"><a href="selenium.html#初始化浏览器对象"><i class="fa fa-check"></i><b>16.2.1</b> 初始化浏览器对象</a></li>
<li class="chapter" data-level="16.2.2" data-path="selenium.html"><a href="selenium.html#访问页面"><i class="fa fa-check"></i><b>16.2.2</b> 访问页面</a></li>
<li class="chapter" data-level="16.2.3" data-path="selenium.html"><a href="selenium.html#查找节点"><i class="fa fa-check"></i><b>16.2.3</b> 查找节点</a></li>
<li class="chapter" data-level="16.2.4" data-path="selenium.html"><a href="selenium.html#节点操作"><i class="fa fa-check"></i><b>16.2.4</b> 节点操作</a></li>
<li class="chapter" data-level="16.2.5" data-path="selenium.html"><a href="selenium.html#执行javascript"><i class="fa fa-check"></i><b>16.2.5</b> 执行JavaScript</a></li>
<li class="chapter" data-level="16.2.6" data-path="selenium.html"><a href="selenium.html#获取节点信息"><i class="fa fa-check"></i><b>16.2.6</b> 获取节点信息</a></li>
<li class="chapter" data-level="16.2.7" data-path="selenium.html"><a href="selenium.html#延时等待"><i class="fa fa-check"></i><b>16.2.7</b> 延时等待</a></li>
</ul></li>
<li class="chapter" data-level="16.3" data-path="selenium.html"><a href="selenium.html#扩展阅读"><i class="fa fa-check"></i><b>16.3</b> 扩展阅读</a></li>
</ul></li>
<li class="part"><span><b>IV 网络应用开发</b></span></li>
<li class="chapter" data-level="17" data-path="django.html"><a href="django.html"><i class="fa fa-check"></i><b>17</b> Django 框架的安装</a><ul>
<li class="chapter" data-level="17.1" data-path="django.html"><a href="django.html#安装-2"><i class="fa fa-check"></i><b>17.1</b> 安装</a><ul>
<li class="chapter" data-level="17.1.1" data-path="django.html"><a href="django.html#安装-django"><i class="fa fa-check"></i><b>17.1.1</b> 安装 Django</a></li>
<li class="chapter" data-level="17.1.2" data-path="django.html"><a href="django.html#验证安装"><i class="fa fa-check"></i><b>17.1.2</b> 验证安装</a></li>
<li class="chapter" data-level="17.1.3" data-path="django.html"><a href="django.html#创建项目并预览效果"><i class="fa fa-check"></i><b>17.1.3</b> 创建项目并预览效果</a></li>
</ul></li>
<li class="chapter" data-level="17.2" data-path="django.html"><a href="django.html#学习资源-1"><i class="fa fa-check"></i><b>17.2</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="quicktutorial.html"><a href="quicktutorial.html"><i class="fa fa-check"></i><b>18</b> 快速上手案例</a><ul>
<li class="chapter" data-level="18.1" data-path="quicktutorial.html"><a href="quicktutorial.html#在-django-项目中创建-app"><i class="fa fa-check"></i><b>18.1</b> 在 Django 项目中创建 APP</a><ul>
<li class="chapter" data-level="18.1.1" data-path="quicktutorial.html"><a href="quicktutorial.html#初始化-app"><i class="fa fa-check"></i><b>18.1.1</b> 初始化 APP</a></li>
<li class="chapter" data-level="18.1.2" data-path="quicktutorial.html"><a href="quicktutorial.html#将应用添加到项目中"><i class="fa fa-check"></i><b>18.1.2</b> 将应用添加到项目中</a></li>
<li class="chapter" data-level="18.1.3" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图"><i class="fa fa-check"></i><b>18.1.3</b> 创建视图</a></li>
<li class="chapter" data-level="18.1.4" data-path="quicktutorial.html"><a href="quicktutorial.html#规划路由"><i class="fa fa-check"></i><b>18.1.4</b> 规划路由</a></li>
<li class="chapter" data-level="18.1.5" data-path="quicktutorial.html"><a href="quicktutorial.html#预览效果"><i class="fa fa-check"></i><b>18.1.5</b> 预览效果</a></li>
</ul></li>
<li class="chapter" data-level="18.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模型的创建及使用"><i class="fa fa-check"></i><b>18.2</b> 模型的创建及使用</a><ul>
<li class="chapter" data-level="18.2.1" data-path="quicktutorial.html"><a href="quicktutorial.html#配置项目信息"><i class="fa fa-check"></i><b>18.2.1</b> 配置项目信息</a></li>
<li class="chapter" data-level="18.2.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型"><i class="fa fa-check"></i><b>18.2.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.2.3" data-path="quicktutorial.html"><a href="quicktutorial.html#激活模型"><i class="fa fa-check"></i><b>18.2.3</b> 激活模型</a></li>
<li class="chapter" data-level="18.2.4" data-path="quicktutorial.html"><a href="quicktutorial.html#数据库的生成与迁移"><i class="fa fa-check"></i><b>18.2.4</b> 数据库的生成与迁移</a></li>
</ul></li>
<li class="chapter" data-level="18.3" data-path="quicktutorial.html"><a href="quicktutorial.html#后台管理"><i class="fa fa-check"></i><b>18.3</b> 后台管理</a><ul>
<li class="chapter" data-level="18.3.1" data-path="quicktutorial.html"><a href="quicktutorial.html#创建管理员账号"><i class="fa fa-check"></i><b>18.3.1</b> 创建管理员账号</a></li>
<li class="chapter" data-level="18.3.2" data-path="quicktutorial.html"><a href="quicktutorial.html#添加引用到后台管理"><i class="fa fa-check"></i><b>18.3.2</b> 添加引用到后台管理</a></li>
<li class="chapter" data-level="18.3.3" data-path="quicktutorial.html"><a href="quicktutorial.html#进入后台管理界面"><i class="fa fa-check"></i><b>18.3.3</b> 进入后台管理界面</a></li>
</ul></li>
<li class="chapter" data-level="18.4" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图-1"><i class="fa fa-check"></i><b>18.4</b> 创建视图</a><ul>
<li class="chapter" data-level="18.4.1" data-path="quicktutorial.html"><a href="quicktutorial.html#添加多个视图"><i class="fa fa-check"></i><b>18.4.1</b> 添加多个视图</a></li>
<li class="chapter" data-level="18.4.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模版命名空间"><i class="fa fa-check"></i><b>18.4.2</b> 模版命名空间</a></li>
<li class="chapter" data-level="18.4.3" data-path="quicktutorial.html"><a href="quicktutorial.html#在视图中使用模板"><i class="fa fa-check"></i><b>18.4.3</b> 在视图中使用模板</a></li>
<li class="chapter" data-level="18.4.4" data-path="quicktutorial.html"><a href="quicktutorial.html#url命名空间"><i class="fa fa-check"></i><b>18.4.4</b> URL命名空间</a></li>
</ul></li>
<li class="chapter" data-level="18.5" data-path="quicktutorial.html"><a href="quicktutorial.html#模型"><i class="fa fa-check"></i><b>18.5</b> 模型</a><ul>
<li class="chapter" data-level="18.5.1" data-path="quicktutorial.html"><a href="quicktutorial.html#设置数据库信息-1"><i class="fa fa-check"></i><b>18.5.1</b> 设置数据库信息</a></li>
<li class="chapter" data-level="18.5.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型-1"><i class="fa fa-check"></i><b>18.5.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.5.3" data-path="quicktutorial.html"><a href="quicktutorial.html#使用模型"><i class="fa fa-check"></i><b>18.5.3</b> 使用模型</a></li>
</ul></li>
<li class="chapter" data-level="18.6" data-path="quicktutorial.html"><a href="quicktutorial.html#参考文献-1"><i class="fa fa-check"></i><b>18.6</b> 参考文献</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://bookdown.org" target="blank">本书由 bookdown 强力驱动</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Python 编程</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="systax" class="section level1">
<h1><span class="header-section-number">第 2 章</span> 核心语法</h1>
<p>以下对Python的核心语法进行介绍。</p>
<div id="注释" class="section level2">
<h2><span class="header-section-number">2.1</span> 注释</h2>
<p>在 Python 中，使用<code>#</code>标记注释。注释不会被 Python 解释器执行。注释是开发人员用来提醒自己或他人程序如何工作的重要手段，注释还会用在文档的写作中。</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb16-1"><a href="systax.html#cb16-1"></a><span class="co"># -*- coding: utf-8 -*-</span></span>
<span id="cb16-2"><a href="systax.html#cb16-2"></a><span class="co"># 注释不会运行</span></span>
<span id="cb16-3"><a href="systax.html#cb16-3"></a><span class="bu">print</span>(<span class="st">&#39;hello world&#39;</span>)</span></code></pre></div>
<p>上述代码将会打印出 <code>hello world</code> 字符串。</p>
<p>Python 之中暗含这样一种期望：Python 鼓励每一行使用一句独立语句从而使得代码更加可读。</p>
<p>所谓物理行（Physical Line）是你在编写程序时你所看到的内容。所谓逻辑行（Logical Line）是 Python 所看到的单个语句。Python 会假定每一物理行会对应一个逻辑行。有关逻辑行的一个例子是诸如 print(‘hello world’) 这样的一行语句，如果其本身是一行（正如你在编辑器里所看到的那样），那么它也对应着一行物理行。</p>

</div>
<div id="变量" class="section level2">
<h2><span class="header-section-number">2.2</span> 变量</h2>
<p>变量（Vaiable）实质上是对内存中地址的命名，在内存中存储着诸多对象，为了方便使用这些对象，便有了变量。把变量和函数的名称我们叫作标识符（Identifier）。</p>
<div id="变量名称" class="section level3">
<h3><span class="header-section-number">2.2.1</span> 变量名称</h3>
<p>在Python中，标识符必须遵守以下规则：</p>
<ol style="list-style-type: decimal">
<li>所有标识符必须以字母或者下划线(<code>_</code>)开头，不能以数字开头。如 my_var 就是一个有效的标识符，而 1digit 就不是。</li>
<li>标识符可以包含字母、数字和下划线。标识符不限长度。</li>
<li>标识符不能是关键字（所谓关键字，就是Python中已经使用并有特定含义的单词）。Python的关键字参见附录Python关键字。</li>
</ol>
</div>
<div id="变量赋值" class="section level3">
<h3><span class="header-section-number">2.2.2</span> 变量赋值</h3>
<p>值（Value）是程序运行过程中的基本元素之一，例如1，3.14，“hello”等等都是值。在编程属于中，它们又被叫作字面量（literals）。字面量拥有不同的类型，如1是整型（int），3.14是浮点型（float），“hello”是字符串（string）。</p>
<p>在Python中，无需声明变量类型，解释器会根据变量的值自动判断变量类型。使用等于号为变量赋值，等于号也被认为赋值操作符（operator）。以下是变量声明的一些例子：</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb17-1"><a href="systax.html#cb17-1"></a>x <span class="op">=</span> <span class="dv">100</span>                       <span class="co"># x 是整型</span></span>
<span id="cb17-2"><a href="systax.html#cb17-2"></a>pi <span class="op">=</span> <span class="fl">3.14</span>                     <span class="co"># pi 是浮点类型</span></span>
<span id="cb17-3"><a href="systax.html#cb17-3"></a>empname <span class="op">=</span> <span class="st">&quot;python is great&quot;</span>   <span class="co"># empname 是字符串</span></span>
<span id="cb17-4"><a href="systax.html#cb17-4"></a>a <span class="op">=</span> b <span class="op">=</span> c <span class="op">=</span> <span class="dv">100</span>               <span class="co"># 将100赋值给a、b、c</span></span></code></pre></div>
<p>注意，变量x中并不储存100自身，它存储的是100（它是一个整型对象）的引用（reference）地址。</p>
</div>
<div id="同步赋值" class="section level3">
<h3><span class="header-section-number">2.2.3</span> 同步赋值</h3>
<p>Python可以使用以下语法对多个变量同步赋值：</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb18-1"><a href="systax.html#cb18-1"></a>var1, var2, ..., varn <span class="op">=</span> exp1, exp2, ..., expn</span></code></pre></div>
<p>上述声明告诉Python，将表达式右边的值依次赋值给表达式左侧的变量。同步赋值在要交换两个变量的值时非常有用。例如：</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb19-1"><a href="systax.html#cb19-1"></a>x <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb19-2"><a href="systax.html#cb19-2"></a>y <span class="op">=</span> <span class="dv">2</span></span>
<span id="cb19-3"><a href="systax.html#cb19-3"></a></span>
<span id="cb19-4"><a href="systax.html#cb19-4"></a>y, x <span class="op">=</span> x, y <span class="co"># 交换x、y的值</span></span>
<span id="cb19-5"><a href="systax.html#cb19-5"></a></span>
<span id="cb19-6"><a href="systax.html#cb19-6"></a><span class="bu">print</span>(x)</span>
<span id="cb19-7"><a href="systax.html#cb19-7"></a><span class="dv">2</span></span>
<span id="cb19-8"><a href="systax.html#cb19-8"></a><span class="bu">print</span>(y)</span>
<span id="cb19-9"><a href="systax.html#cb19-9"></a><span class="dv">1</span></span></code></pre></div>

</div>
</div>
<div id="数字类型" class="section level2">
<h2><span class="header-section-number">2.3</span> 数字类型</h2>
<p>Python 3 支持3种不同类型的数字类型。</p>
<ol style="list-style-type: decimal">
<li><code>int</code> 整型数字，比如<code>2015</code>。</li>
<li><code>float</code> 浮点型数字，比如<code>3.14</code>。</li>
<li><code>complex</code> 复数，比如<code>3+2j</code>。</li>
</ol>
<div id="查看变量类型" class="section level3">
<h3><span class="header-section-number">2.3.1</span> 查看变量类型</h3>
<p>Python 使用内置函数 <code>type()</code>来查看变量的类型。在Python中，内置了一些高效强大的对象类型，使得开发人员不用从零开始进行编程。实际上，Python中的每样东西都是对象。虽然Python中没有类型声明，但表达式的语法决定了创建和使用的对象的类型。一旦创建了一个对象，它就和操作集合绑定了，这就是所谓的动态类型和强类型语言。即Python自动根据表达式创建类型，一旦创建成功，只能对一个对象进行适合该类型的有效操作。</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb20-1"><a href="systax.html#cb20-1"></a><span class="op">&gt;&gt;&gt;</span> x <span class="op">=</span> <span class="dv">12</span></span>
<span id="cb20-2"><a href="systax.html#cb20-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">type</span>(x)</span>
<span id="cb20-3"><a href="systax.html#cb20-3"></a> <span class="op">&lt;</span><span class="kw">class</span> <span class="st">&#39;int&#39;</span><span class="op">&gt;</span></span></code></pre></div>
</div>
<div id="整型" class="section level3">
<h3><span class="header-section-number">2.3.2</span> 整型</h3>
<p>整型（int）字面量在Python中属于<code>int</code>类。</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb21-1"><a href="systax.html#cb21-1"></a><span class="op">&gt;&gt;&gt;</span> i <span class="op">=</span> <span class="dv">100</span></span>
<span id="cb21-2"><a href="systax.html#cb21-2"></a><span class="op">&gt;&gt;&gt;</span> i</span>
<span id="cb21-3"><a href="systax.html#cb21-3"></a><span class="dv">100</span></span></code></pre></div>
<p>数字可以进行各种运算，如：</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb22-1"><a href="systax.html#cb22-1"></a><span class="dv">123</span> <span class="op">+</span> <span class="dv">345</span></span></code></pre></div>
<p>还可以使用数学模块进行更高级的运算，如产生随机数等等：</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb23-1"><a href="systax.html#cb23-1"></a><span class="im">import</span> random</span>
<span id="cb23-2"><a href="systax.html#cb23-2"></a><span class="bu">print</span>(random.random())</span></code></pre></div>
<p><code>import</code>表示引入模块，<code>import random</code>就是引入随机数模块。</p>
</div>
<div id="浮点类型" class="section level3">
<h3><span class="header-section-number">2.3.3</span> 浮点类型</h3>
<p>浮点数（float）是指有小数点的数字。</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb24-1"><a href="systax.html#cb24-1"></a><span class="op">&gt;&gt;&gt;</span> f <span class="op">=</span> <span class="fl">12.3</span></span>
<span id="cb24-2"><a href="systax.html#cb24-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">type</span>(f)</span>
<span id="cb24-3"><a href="systax.html#cb24-3"></a><span class="op">&lt;</span><span class="kw">class</span> <span class="st">&#39;float&#39;</span><span class="op">&gt;</span></span></code></pre></div>
</div>
<div id="复数" class="section level3">
<h3><span class="header-section-number">2.3.4</span> 复数</h3>
<p>复数（Complex number）由实数和虚数两部分构成，虚数用j表示。我们可以这样定义一个复数：</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb25-1"><a href="systax.html#cb25-1"></a><span class="op">&gt;&gt;&gt;</span> x <span class="op">=</span> <span class="dv">2</span><span class="op">+</span>3j</span>
<span id="cb25-2"><a href="systax.html#cb25-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">type</span>(x)</span>
<span id="cb25-3"><a href="systax.html#cb25-3"></a><span class="op">&lt;</span><span class="kw">class</span> <span class="st">&#39;complex&#39;</span><span class="op">&gt;</span></span></code></pre></div>

</div>
</div>
<div id="运算符" class="section level2">
<h2><span class="header-section-number">2.4</span> 运算符</h2>
<p>Python有各种运算符，我们可以使用这些运算符完成计算。运算符见下表:</p>
<table>
<thead>
<tr class="header">
<th>名称</th>
<th>含义</th>
<th>例子</th>
<th>运行结果</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>+</td>
<td>加</td>
<td>3+1</td>
<td>4</td>
</tr>
<tr class="even">
<td>-</td>
<td>减</td>
<td>40-2</td>
<td>38</td>
</tr>
<tr class="odd">
<td><span class="math inline">\(*\)</span></td>
<td>乘</td>
<td>3*2</td>
<td>6</td>
</tr>
<tr class="even">
<td>/</td>
<td>除</td>
<td>6/3</td>
<td>2</td>
</tr>
<tr class="odd">
<td>//</td>
<td>取整除</td>
<td>3//2</td>
<td>1</td>
</tr>
<tr class="even">
<td>**</td>
<td>幂</td>
<td>2**3</td>
<td>8</td>
</tr>
<tr class="odd">
<td>%</td>
<td>求余数</td>
<td>7%2</td>
<td>1</td>
</tr>
</tbody>
</table>
<div id="运算符的优先级别" class="section level3">
<h3><span class="header-section-number">2.4.1</span> 运算符的优先级别</h3>
<p>Python按照运算符的有限级别计算表达式的值，比如：</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb26-1"><a href="systax.html#cb26-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="dv">3</span> <span class="op">*</span> <span class="dv">4</span> <span class="op">+</span> <span class="dv">1</span></span></code></pre></div>
<p>在上面的表达式中，应该先进行加运算还是乘运算？为了搞清楚这个问题，我们需要明白Python中运算符的优先级别，表显示了运算符的优先级别，依次从高到底排列如下：</p>
<table>
<thead>
<tr class="header">
<th>运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>‘expression,…’</td>
<td>字符串转换</td>
</tr>
<tr class="even">
<td>{key:datum,…}</td>
<td>字典显示</td>
</tr>
<tr class="odd">
<td>{[expression,…]}</td>
<td>列表显示</td>
</tr>
<tr class="even">
<td>()</td>
<td>分组</td>
</tr>
<tr class="odd">
<td>f(args…)</td>
<td>函数调用</td>
</tr>
<tr class="even">
<td>x[index:index]</td>
<td>列表切分</td>
</tr>
<tr class="odd">
<td>x[index]</td>
<td>元素下标</td>
</tr>
<tr class="even">
<td>x.attr</td>
<td>调用对象属性</td>
</tr>
<tr class="odd">
<td><span class="math inline">\(**\)</span></td>
<td>指数运算</td>
</tr>
<tr class="even">
<td>{^x}</td>
<td>按位取反</td>
</tr>
<tr class="odd">
<td>+x,-x</td>
<td>正负号</td>
</tr>
<tr class="even">
<td><span class="math inline">\(*\)</span>，/，%</td>
<td>乘、除、取余数</td>
</tr>
<tr class="odd">
<td>+，-</td>
<td>加，减</td>
</tr>
<tr class="even">
<td>&lt;&lt;，&gt;&gt;</td>
<td>逐位左移，逐位右移</td>
</tr>
<tr class="odd">
<td>&amp;</td>
<td>逐位求和</td>
</tr>
<tr class="even">
<td>^</td>
<td>逐位异或</td>
</tr>
<tr class="odd">
<td>|</td>
<td>逐位或</td>
</tr>
<tr class="even">
<td>&lt;,&lt;=,&gt;,&gt;=,&lt;&gt;,!=,==</td>
<td>比较</td>
</tr>
<tr class="odd">
<td>is,not is</td>
<td>同一性测试</td>
</tr>
<tr class="even">
<td>in,not in</td>
<td>成员资格判断</td>
</tr>
<tr class="odd">
<td>not x</td>
<td>布尔“非”</td>
</tr>
<tr class="even">
<td>and</td>
<td>布尔“并”</td>
</tr>
<tr class="odd">
<td>or</td>
<td>布尔“或”</td>
</tr>
<tr class="even">
<td>lambda</td>
<td>Lamada表达式</td>
</tr>
</tbody>
</table>
<p>在上表中我们可以看到，乘法运算的级别高于加法，因此，先进行乘法运算，再进行加法运算，最后的计算结果为13。</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb27-1"><a href="systax.html#cb27-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="dv">3</span> <span class="op">*</span> <span class="dv">4</span> <span class="op">+</span> <span class="dv">1</span></span>
<span id="cb27-2"><a href="systax.html#cb27-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="dv">13</span></span></code></pre></div>
<p>让我们再看下面的例子，以便演示优先顺序的另一个问题：</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb28-1"><a href="systax.html#cb28-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="dv">3</span> <span class="op">+</span> <span class="dv">4</span> <span class="op">-</span> <span class="dv">2</span></span></code></pre></div>
<p>上述表达式到底先进行加法运算还是减法呢？因为在运算符的优先级别表中我们看到加减运算的优先级别相同。当优先级别相同时，表达式从左向右计算，也就是说，上述的例子将先进行加法运算，再进行减法运算。</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb29-1"><a href="systax.html#cb29-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="dv">3</span> <span class="op">+</span> <span class="dv">4</span> <span class="op">-</span> <span class="dv">2</span></span>
<span id="cb29-2"><a href="systax.html#cb29-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="dv">5</span></span></code></pre></div>
<p>同级别运算符从左到右运算，这条规则有个例外，那就是赋值运算( = )，赋值运算是从右向左计算的。例如：</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb30-1"><a href="systax.html#cb30-1"></a>a <span class="op">=</span> b <span class="op">=</span> c</span></code></pre></div>
<p>先将c的值，赋给b，再将b的值赋给a。</p>
</div>
<div id="增强赋值运算符" class="section level3">
<h3><span class="header-section-number">2.4.2</span> 增强赋值运算符</h3>
<p>增强赋值运算符能简化赋值声明语句，例如：</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb31-1"><a href="systax.html#cb31-1"></a><span class="op">&gt;&gt;&gt;</span> count <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb31-2"><a href="systax.html#cb31-2"></a><span class="op">&gt;&gt;&gt;</span> count <span class="op">=</span> count <span class="op">+</span> <span class="dv">1</span></span>
<span id="cb31-3"><a href="systax.html#cb31-3"></a><span class="op">&gt;&gt;&gt;</span> count</span>
<span id="cb31-4"><a href="systax.html#cb31-4"></a><span class="dv">2</span></span></code></pre></div>
<p>使用增强赋值运算符，我们可以将上述代码变为：</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb32-1"><a href="systax.html#cb32-1"></a><span class="op">&gt;&gt;&gt;</span> count <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb32-2"><a href="systax.html#cb32-2"></a><span class="op">&gt;&gt;&gt;</span> count <span class="op">+=</span> <span class="dv">1</span></span>
<span id="cb32-3"><a href="systax.html#cb32-3"></a><span class="op">&gt;&gt;&gt;</span> count</span>
<span id="cb32-4"><a href="systax.html#cb32-4"></a><span class="dv">2</span></span></code></pre></div>
<p>类似的增强赋值运算符，除了<code>+=</code>外，还有<code>-=</code>，<code>%=</code>，<code>/=</code> 等等。</p>

</div>
</div>
<div id="序列" class="section level2">
<h2><span class="header-section-number">2.5</span> 序列</h2>
<p>序列（Sequence）是一个包含其他对象的有序集合，序列中的元素包含了一个从左到右的顺序，可以根据元素所在的位置进行存储和读取。Python中内建了6种序列，分别是列表、元组、字符串、unicode字符串、buffer对象和xrange对象。</p>
<p>序列作为Python的数据结构，有一些操作是通用的，如：索引、分片、加、乘以及检查某个成员是否属于序列的成员（成员资格），另外，还有一些计算长度、找到最大元素等等的内建函数。</p>
<div id="索引" class="section level3">
<h3><span class="header-section-number">2.5.1</span> 索引</h3>
<p>序列中的所有元素都有编号，从0开始，可以按照编号来访问序列中的元素，这个标号就是索引(indexing)。</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb33-1"><a href="systax.html#cb33-1"></a>se <span class="op">=</span> <span class="st">&#39;Hello&#39;</span></span>
<span id="cb33-2"><a href="systax.html#cb33-2"></a><span class="bu">print</span>(se[<span class="dv">0</span>])</span>
<span id="cb33-3"><a href="systax.html#cb33-3"></a><span class="bu">print</span>(se[<span class="op">-</span><span class="dv">1</span>])</span></code></pre></div>
<p>se[0]表示序列se中的第一个元素，se[-1]表示序列中的最后一个元素。</p>
</div>
<div id="分片" class="section level3">
<h3><span class="header-section-number">2.5.2</span> 分片</h3>
<p>分片（Slicing）操作指的是访问序列中一定范围之内的元素。分片通过冒号相隔的两个索引来实现，第一个索引是需要提取部分的第1个元素的编号，而第二个索引是分片之后剩下部分的第1个元素的编号，第二个索引不包含在分片之中：</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb34-1"><a href="systax.html#cb34-1"></a>se <span class="op">=</span> <span class="st">&#39;Hello Pythoner！&#39;</span></span>
<span id="cb34-2"><a href="systax.html#cb34-2"></a><span class="st">print(se[0:5])</span></span></code></pre></div>
<p>上述代码将打印出‘Hello’字符串。但有时，我们需要获取序列的后面几个元素，同时，序列的大小是未知的，我们可以这样写：</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb35-1"><a href="systax.html#cb35-1"></a>se <span class="op">=</span> <span class="st">&#39;Hello Pythoner！&#39;</span></span>
<span id="cb35-2"><a href="systax.html#cb35-2"></a><span class="st">print(se[-9:])</span></span></code></pre></div>
<p>se[-9:]中空了第2个索引，表示一直到最后一个元素。上述代码将打印出‘Pythoner！’字符串。</p>
<p>进行分片时，分片的开始和结束点需要指定。而另外一个参数步长（step length）通常默认为1，当有必要时，可是指定切片的步长，如每隔1个元素就取出元素：</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb36-1"><a href="systax.html#cb36-1"></a>numbers <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>, <span class="dv">10</span>]</span>
<span id="cb36-2"><a href="systax.html#cb36-2"></a><span class="bu">print</span>(numbers[<span class="dv">0</span>:<span class="dv">10</span>:<span class="dv">2</span>])</span>
<span id="cb36-3"><a href="systax.html#cb36-3"></a><span class="bu">print</span>(numbers[<span class="dv">1</span>::<span class="dv">2</span>])</span></code></pre></div>
<p>上述代码将打印出‘[1, 3, 5, 7, 9]’和‘[2, 4, 6, 8, 10]’，其中的步长都是2。
当然步长也可以设置为负值，这样分片会从后往前进行。</p>
</div>
<div id="序列相加" class="section level3">
<h3><span class="header-section-number">2.5.3</span> 序列相加</h3>
<p>可以通过加号能对两个相同类型的序列进行连接运算，如字符串：</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb37-1"><a href="systax.html#cb37-1"></a>hello <span class="op">=</span> <span class="st">&#39;你好&#39;</span></span>
<span id="cb37-2"><a href="systax.html#cb37-2"></a>name <span class="op">=</span> <span class="st">&#39;yangjh&#39;</span></span>
<span id="cb37-3"><a href="systax.html#cb37-3"></a><span class="bu">print</span>(hello <span class="op">+</span> name)</span></code></pre></div>
<p>上述代码将打印出‘你好yangjh’字符串。</p>
</div>
<div id="序列相乘" class="section level3">
<h3><span class="header-section-number">2.5.4</span> 序列相乘</h3>
<p>序列乘以数字，表示将原有序列重复若干次：</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb38-1"><a href="systax.html#cb38-1"></a>hello <span class="op">=</span> <span class="st">&#39;你好&#39;</span></span>
<span id="cb38-2"><a href="systax.html#cb38-2"></a><span class="bu">print</span>(hello <span class="op">*</span> <span class="dv">3</span>)</span></code></pre></div>
<p>上述代码将打印出‘你好你好你好’字符串。</p>
<p>空列表可以使用‘[]’来表示，但是，如果想创建有10个空元素组成的列表，就需要使用None，None是Python内建的一个值，表示什么都没有，因此，要创建含有10个空元素的列表，就可以这样：</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb39-1"><a href="systax.html#cb39-1"></a><span class="bu">print</span>([<span class="va">None</span>] <span class="op">*</span> <span class="dv">10</span>)</span></code></pre></div>
</div>
<div id="成员资格" class="section level3">
<h3><span class="header-section-number">2.5.5</span> 成员资格</h3>
<p>使用in运算符，可以检查某个元素是否存在与指定的序列中。如果元素存在于序列中，则返回True，否则返回False。</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb40-1"><a href="systax.html#cb40-1"></a><span class="bu">print</span>(<span class="st">&#39;张三&#39;</span> <span class="kw">in</span> [<span class="st">&#39;张三&#39;</span>, <span class="st">&#39;李四&#39;</span>, <span class="st">&#39;王二&#39;</span>])</span></code></pre></div>
<p>上述代码将打印出布尔值True。</p>
</div>
<div id="长度最小值最大值" class="section level3">
<h3><span class="header-section-number">2.5.6</span> 长度、最小值、最大值</h3>
<p>dir()函数可以输出对象的内置方法。如：dir(‘str’)就可以打印出所有字符串对象的内置方法。</p>
<p>内建函数len()可以返回序列的大小，如：</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb41-1"><a href="systax.html#cb41-1"></a>numbers <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>, <span class="dv">10</span>]</span>
<span id="cb41-2"><a href="systax.html#cb41-2"></a><span class="bu">print</span>(<span class="bu">len</span>(numbers))</span>
<span id="cb41-3"><a href="systax.html#cb41-3"></a><span class="bu">print</span>(<span class="bu">max</span>(numbers))</span>
<span id="cb41-4"><a href="systax.html#cb41-4"></a><span class="bu">print</span>(<span class="bu">min</span>(numbers))</span></code></pre></div>
<p>上述代码将打印出<code>numbers</code>序列的长度<code>10</code>和最大值以及最小值。</p>

</div>
</div>
<div id="字符串" class="section level2">
<h2><span class="header-section-number">2.6</span> 字符串</h2>
<p>Python 中的字符串（Strings）是用单引号或双引号标记的一系列连续字符（characters），换句话说，字符串是由单个字符组成的序列（list）。即便只有一个字符，也是字符串，Python 中没有字符数据类型。记住单引号括起的字符串和双引号括起的字符串是一样的，它们不存在任何区别。</p>
<div id="创建字符串" class="section level3">
<h3><span class="header-section-number">2.6.1</span> 创建字符串</h3>
<div class="sourceCode" id="cb42"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb42-1"><a href="systax.html#cb42-1"></a><span class="op">&gt;&gt;&gt;</span> name <span class="op">=</span> <span class="st">&quot;tom&quot;</span></span>
<span id="cb42-2"><a href="systax.html#cb42-2"></a><span class="op">&gt;&gt;&gt;</span> mychar <span class="op">=</span> <span class="st">&#39;a&#39;</span></span></code></pre></div>
<p>我们还可以使用下面的语法创建字符串：</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb43-1"><a href="systax.html#cb43-1"></a><span class="op">&gt;&gt;&gt;</span> name1 <span class="op">=</span> <span class="bu">str</span>() <span class="co"># 创建一个空字符串</span></span>
<span id="cb43-2"><a href="systax.html#cb43-2"></a><span class="op">&gt;&gt;&gt;</span> name2 <span class="op">=</span> <span class="bu">str</span>(<span class="st">&quot;newstring&quot;</span>) <span class="co"># 创建一个内容为 newstring 的字符串</span></span></code></pre></div>
</div>
<div id="字符串的不可变性" class="section level3">
<h3><span class="header-section-number">2.6.2</span> 字符串的不可变性</h3>
<p>在 Python 中，每一个对象都可以分为不可变性或者可变性。在核心类型中，数字、字符串和元组是不可变的。</p>
<p>字符串在 Python 中一旦创建就不能就地改变，例如不能通过对其某一位置进行赋值而改变字符串。下面的语句就会出现如下语法错误：“TypeError: ‘str’ object does not support item assignment”。</p>
<div class="sourceCode" id="cb44"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb44-1"><a href="systax.html#cb44-1"></a>s <span class="op">=</span> <span class="st">&#39;string&#39;</span></span>
<span id="cb44-2"><a href="systax.html#cb44-2"></a><span class="bu">print</span>(<span class="bu">len</span>(s))</span>
<span id="cb44-3"><a href="systax.html#cb44-3"></a><span class="bu">print</span>(s[<span class="dv">0</span>])                 <span class="co"># 输出序列的第一个元素</span></span>
<span id="cb44-4"><a href="systax.html#cb44-4"></a>s[<span class="dv">0</span>] <span class="op">=</span> <span class="st">&#39;another s&#39;</span>          <span class="co"># 试图修改字符串的内容</span></span>
<span id="cb44-5"><a href="systax.html#cb44-5"></a><span class="bu">print</span>(s)</span></code></pre></div>
<p>关于不可变性，我们再看一个例子：</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb45-1"><a href="systax.html#cb45-1"></a><span class="op">&gt;&gt;&gt;</span> str1 <span class="op">=</span> <span class="st">&quot;welcome&quot;</span></span>
<span id="cb45-2"><a href="systax.html#cb45-2"></a><span class="op">&gt;&gt;&gt;</span> str2 <span class="op">=</span> <span class="st">&quot;welcome&quot;</span></span></code></pre></div>
<p>上述代码中，str1 和 str2 都指向存储在内存中某个地方的字符串对象“welcome”。我们可以通过 id() 函数来测试 str1 和 str2 是否真的指向同一个对象。</p>
<p><code>id()</code> 函数可以得到对象在内存中的存储地址。</p>
<p>如下：</p>
<div class="sourceCode" id="cb46"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb46-1"><a href="systax.html#cb46-1"></a><span class="op">&gt;&gt;&gt;</span> str1 <span class="op">=</span> <span class="st">&#39;welcome&#39;</span></span>
<span id="cb46-2"><a href="systax.html#cb46-2"></a><span class="op">&gt;&gt;&gt;</span> str2 <span class="op">=</span> <span class="st">&#39;welcome&#39;</span></span>
<span id="cb46-3"><a href="systax.html#cb46-3"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">id</span>(str1)</span>
<span id="cb46-4"><a href="systax.html#cb46-4"></a><span class="dv">35462112</span></span>
<span id="cb46-5"><a href="systax.html#cb46-5"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">id</span>(str2)</span>
<span id="cb46-6"><a href="systax.html#cb46-6"></a><span class="dv">35462112</span></span></code></pre></div>
<p>我们可以看到，str1 和 str2 都指向同一个内存地址，因此，他们都指向同样的对象“welcome”。下面让我们再编辑 str1 的内容看看：</p>
<div class="sourceCode" id="cb47"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb47-1"><a href="systax.html#cb47-1"></a><span class="op">&gt;&gt;&gt;</span> str1 <span class="op">+=</span> <span class="st">&quot; yangjh&quot;</span></span>
<span id="cb47-2"><a href="systax.html#cb47-2"></a><span class="op">&gt;&gt;&gt;</span> str1</span>
<span id="cb47-3"><a href="systax.html#cb47-3"></a><span class="co">&#39;welcome yangjh&#39;</span></span>
<span id="cb47-4"><a href="systax.html#cb47-4"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">id</span>(str1)</span>
<span id="cb47-5"><a href="systax.html#cb47-5"></a><span class="dv">35487600</span></span></code></pre></div>
<p>我们可以看到，现在变量 str1 指向了一个完全不同的内容地址，这也说明，我们对 str1 的内容操作实际上是新建了一个新的字符串对象。</p>
</div>
<div id="字符串操作" class="section level3">
<h3><span class="header-section-number">2.6.3</span> 字符串操作</h3>
<p>字符串索引开始于 0，因此，我们可以这样获取字符串的第一个字符：</p>
<div class="sourceCode" id="cb48"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb48-1"><a href="systax.html#cb48-1"></a><span class="op">&gt;&gt;&gt;</span> name <span class="op">=</span> <span class="st">&#39;yangjh&#39;</span></span>
<span id="cb48-2"><a href="systax.html#cb48-2"></a><span class="op">&gt;&gt;&gt;</span> name[<span class="dv">0</span>]</span>
<span id="cb48-3"><a href="systax.html#cb48-3"></a><span class="co">&#39;y&#39;</span></span></code></pre></div>
<p>在对字符串操作时，还可以从后往前取元素：</p>
<div class="sourceCode" id="cb49"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb49-1"><a href="systax.html#cb49-1"></a><span class="op">&gt;&gt;&gt;</span> name[<span class="op">-</span><span class="dv">1</span>]</span>
<span id="cb49-2"><a href="systax.html#cb49-2"></a><span class="co">&#39;h&#39;</span></span></code></pre></div>
<p>运算符“+”用来连接字符串，运算符“*”用来重复字符串，例如：</p>
<div class="sourceCode" id="cb50"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb50-1"><a href="systax.html#cb50-1"></a><span class="op">&gt;&gt;&gt;</span> s <span class="op">=</span> <span class="st">&quot;tom and &quot;</span> <span class="op">+</span> <span class="st">&quot;jerry&quot;</span></span>
<span id="cb50-2"><a href="systax.html#cb50-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(s)</span>
<span id="cb50-3"><a href="systax.html#cb50-3"></a>tom <span class="kw">and</span> jerry</span>
<span id="cb50-4"><a href="systax.html#cb50-4"></a><span class="op">&gt;&gt;&gt;</span> s <span class="op">=</span> <span class="st">&quot;love &quot;</span> <span class="op">*</span> <span class="dv">3</span></span>
<span id="cb50-5"><a href="systax.html#cb50-5"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(s)</span>
<span id="cb50-6"><a href="systax.html#cb50-6"></a>love love love</span></code></pre></div>
</div>
<div id="字符串分片" class="section level3">
<h3><span class="header-section-number">2.6.4</span> 字符串分片</h3>
<p>我们还可以通过“[]”操作符来获取原始字符串的子集，这就是所谓的分片。语法规则如下：</p>
<div class="sourceCode" id="cb51"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb51-1"><a href="systax.html#cb51-1"></a>s[start:end]</span></code></pre></div>
<p>切分操作将返回字符串的部分内容，起始于 index，结束于 end-1。例如：</p>
<div class="sourceCode" id="cb52"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb52-1"><a href="systax.html#cb52-1"></a><span class="op">&gt;&gt;&gt;</span> s <span class="op">=</span> <span class="st">&#39;yangjh&#39;</span></span>
<span id="cb52-2"><a href="systax.html#cb52-2"></a><span class="op">&gt;&gt;&gt;</span> s[<span class="dv">1</span>:<span class="dv">3</span>]</span>
<span id="cb52-3"><a href="systax.html#cb52-3"></a><span class="co">&#39;an&#39;</span></span>
<span id="cb52-4"><a href="systax.html#cb52-4"></a><span class="op">&gt;&gt;&gt;</span> s <span class="op">=</span> <span class="st">&quot;Welcome&quot;</span></span>
<span id="cb52-5"><a href="systax.html#cb52-5"></a><span class="op">&gt;&gt;&gt;</span> s[ : <span class="dv">6</span>]</span>
<span id="cb52-6"><a href="systax.html#cb52-6"></a><span class="co">&#39;Welcom&#39;</span></span>
<span id="cb52-7"><a href="systax.html#cb52-7"></a><span class="op">&gt;&gt;&gt;</span> s[<span class="dv">4</span> : ]</span>
<span id="cb52-8"><a href="systax.html#cb52-8"></a><span class="co">&#39;ome&#39;</span></span>
<span id="cb52-9"><a href="systax.html#cb52-9"></a><span class="op">&gt;&gt;&gt;</span> s[<span class="dv">1</span> : <span class="dv">-1</span>]</span>
<span id="cb52-10"><a href="systax.html#cb52-10"></a><span class="co">&#39;elcom&#39;</span></span></code></pre></div>
<p>注意：开始索引和结束索引都是可选的，如果忽略，开始索引就是 0，而结束索引就是字符串的最后一个字符对应的索引值。</p>
</div>
<div id="in-和-not-in-操作符" class="section level3">
<h3><span class="header-section-number">2.6.5</span> in 和 not in 操作符</h3>
<p>我们可以使用 in 和 not in 操作符检查一个字符串是否存在于另一个字符串，in 和 not in 就是所谓的成员资格操作符（membership operator）。</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb53-1"><a href="systax.html#cb53-1"></a><span class="op">&gt;&gt;&gt;</span> s1 <span class="op">=</span> <span class="st">&quot;Welcome&quot;</span></span>
<span id="cb53-2"><a href="systax.html#cb53-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;come&quot;</span> <span class="kw">in</span> s1</span>
<span id="cb53-3"><a href="systax.html#cb53-3"></a><span class="va">True</span></span>
<span id="cb53-4"><a href="systax.html#cb53-4"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;come&quot;</span> <span class="kw">not</span> <span class="kw">in</span> s1</span>
<span id="cb53-5"><a href="systax.html#cb53-5"></a><span class="va">False</span></span></code></pre></div>
</div>
<div id="string-对象的方法" class="section level3">
<h3><span class="header-section-number">2.6.6</span> String 对象的方法</h3>
<p>下表是三个常用的字符串方法：</p>
<table>
<thead>
<tr class="header">
<th>方法名称</th>
<th>功能描述</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>len()</td>
<td>返回字符串长度</td>
</tr>
<tr class="even">
<td>max()</td>
<td>返回字符串中 ASCII 编码值最大的字符</td>
</tr>
<tr class="odd">
<td>min()</td>
<td>返回字符串中 ASCII 编码值最小的字符</td>
</tr>
</tbody>
</table>
<div class="sourceCode" id="cb54"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb54-1"><a href="systax.html#cb54-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">len</span>(<span class="st">&quot;hello&quot;</span>)</span>
<span id="cb54-2"><a href="systax.html#cb54-2"></a><span class="dv">5</span></span>
<span id="cb54-3"><a href="systax.html#cb54-3"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">max</span>(<span class="st">&quot;abc&quot;</span>)</span>
<span id="cb54-4"><a href="systax.html#cb54-4"></a><span class="co">&#39;c&#39;</span></span>
<span id="cb54-5"><a href="systax.html#cb54-5"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">min</span>(<span class="st">&quot;abc&quot;</span>)</span>
<span id="cb54-6"><a href="systax.html#cb54-6"></a><span class="co">&#39;a&#39;</span></span></code></pre></div>
</div>
<div id="比较字符串" class="section level3">
<h3><span class="header-section-number">2.6.7</span> 比较字符串</h3>
<p>我们可以使用 ( &gt; , &lt; , &lt;= , &lt;= , == , != ) 比较两个字符串。Python 比较字符串是按照编纂字典的方式进行的，也就是使用 <a href="http://tool.oschina.net/commons?type=4">ASCII 编码值</a><a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a></p>
<p>假设 str1 的值为“Jane”，str2 的值为“Jake”，首先比较这两个字符串的第一个字符“J”，如果相等，就继续比较第二个字符（a 和 a），因为相同，所以继续比较第三个字符（n 和 k），因为 n 的 ASCII 编码值大于 k，因此 str1 大于 str2。更多例子参见下面的代码：</p>
<div class="sourceCode" id="cb55"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb55-1"><a href="systax.html#cb55-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;tim&quot;</span> <span class="op">==</span> <span class="st">&quot;tie&quot;</span></span>
<span id="cb55-2"><a href="systax.html#cb55-2"></a><span class="va">False</span></span>
<span id="cb55-3"><a href="systax.html#cb55-3"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;free&quot;</span> <span class="op">!=</span> <span class="st">&quot;freedom&quot;</span></span>
<span id="cb55-4"><a href="systax.html#cb55-4"></a><span class="va">True</span></span>
<span id="cb55-5"><a href="systax.html#cb55-5"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;arrow&quot;</span> <span class="op">&gt;</span> <span class="st">&quot;aron&quot;</span></span>
<span id="cb55-6"><a href="systax.html#cb55-6"></a><span class="va">True</span></span>
<span id="cb55-7"><a href="systax.html#cb55-7"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;green&quot;</span> <span class="op">&gt;=</span> <span class="st">&quot;glow&quot;</span></span>
<span id="cb55-8"><a href="systax.html#cb55-8"></a><span class="va">True</span></span>
<span id="cb55-9"><a href="systax.html#cb55-9"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;green&quot;</span> <span class="op">&lt;</span> <span class="st">&quot;glow&quot;</span></span>
<span id="cb55-10"><a href="systax.html#cb55-10"></a><span class="va">False</span></span>
<span id="cb55-11"><a href="systax.html#cb55-11"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;green&quot;</span> <span class="op">&lt;=</span> <span class="st">&quot;glow&quot;</span></span>
<span id="cb55-12"><a href="systax.html#cb55-12"></a><span class="va">False</span></span>
<span id="cb55-13"><a href="systax.html#cb55-13"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;ab&quot;</span> <span class="op">&lt;=</span> <span class="st">&quot;abc&quot;</span></span>
<span id="cb55-14"><a href="systax.html#cb55-14"></a><span class="va">True</span></span></code></pre></div>
</div>
<div id="遍历字符串" class="section level3">
<h3><span class="header-section-number">2.6.8</span> 遍历字符串</h3>
<p>字符串是序列，因此也可以使用循环遍历成员。</p>
<div class="sourceCode" id="cb56"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb56-1"><a href="systax.html#cb56-1"></a><span class="op">&gt;&gt;&gt;</span> s <span class="op">=</span> <span class="st">&quot;yangjh&quot;</span></span>
<span id="cb56-2"><a href="systax.html#cb56-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="cf">for</span> i <span class="kw">in</span> s:</span>
<span id="cb56-3"><a href="systax.html#cb56-3"></a>...     <span class="bu">print</span>(i, end<span class="op">=</span><span class="st">&quot;&quot;</span>)</span>
<span id="cb56-4"><a href="systax.html#cb56-4"></a>...</span>
<span id="cb56-5"><a href="systax.html#cb56-5"></a>yangjh</span></code></pre></div>
<p>改变 print() 函数的输出格式</p>
<p>print() 函数在默认状态下，会另起一行打印字符串，我们可以使用第二个参数修改结束标记。如
print(“my string”, end="") 就表示打印字符串，但不另起一行。</p>
</div>
<div id="字符串内容检验" class="section level3">
<h3><span class="header-section-number">2.6.9</span> 字符串内容检验</h3>
<p>Python 字符串类内置了丰富的方法，使用这些方法（见表、ref{tab: 字符串内容检验}），我们可以检查字符串内容的类型。</p>
<table>
<thead>
<tr class="header">
<th>方法名称</th>
<th>方法说明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>isalnum()</td>
<td>如果 str 包含字符都是字母或数字则返回 True</td>
</tr>
<tr class="even">
<td>isalpha()</td>
<td>如果 string 包含字符都是字母则返回 True</td>
</tr>
<tr class="odd">
<td>isdigit()</td>
<td>如果 string 包含字符都是数字则返回 True</td>
</tr>
<tr class="even">
<td>isidentifier()</td>
<td>判断字符串是否是合格的标识名</td>
</tr>
<tr class="odd">
<td>islower()</td>
<td>判断字符串中是否都是小写字母</td>
</tr>
<tr class="even">
<td>isupper()</td>
<td>判断字符串中是否都是大写字母</td>
</tr>
<tr class="odd">
<td>isspace()</td>
<td>判断字符串是否由空格组成</td>
</tr>
</tbody>
</table>
<p>这些判断方法的实例如下：</p>
<div class="sourceCode" id="cb57"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb57-1"><a href="systax.html#cb57-1"></a><span class="op">&gt;&gt;&gt;</span> s <span class="op">=</span> <span class="st">&quot;welcome to python&quot;</span></span>
<span id="cb57-2"><a href="systax.html#cb57-2"></a><span class="op">&gt;&gt;&gt;</span> s.isalnum()</span>
<span id="cb57-3"><a href="systax.html#cb57-3"></a><span class="va">False</span></span>
<span id="cb57-4"><a href="systax.html#cb57-4"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;Welcome&quot;</span>.isalpha()</span>
<span id="cb57-5"><a href="systax.html#cb57-5"></a><span class="va">True</span></span>
<span id="cb57-6"><a href="systax.html#cb57-6"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;2012&quot;</span>.isdigit()</span>
<span id="cb57-7"><a href="systax.html#cb57-7"></a><span class="va">True</span></span>
<span id="cb57-8"><a href="systax.html#cb57-8"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;first Number&quot;</span>.isidentifier()</span>
<span id="cb57-9"><a href="systax.html#cb57-9"></a><span class="va">False</span></span>
<span id="cb57-10"><a href="systax.html#cb57-10"></a><span class="op">&gt;&gt;&gt;</span> s.islower()</span>
<span id="cb57-11"><a href="systax.html#cb57-11"></a><span class="va">True</span></span>
<span id="cb57-12"><a href="systax.html#cb57-12"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;WELCOME&quot;</span>.isupper()</span>
<span id="cb57-13"><a href="systax.html#cb57-13"></a><span class="va">True</span></span>
<span id="cb57-14"><a href="systax.html#cb57-14"></a><span class="op">&gt;&gt;&gt;</span> <span class="st">&quot;  </span><span class="ch">\t</span><span class="st">&quot;</span>.isspace()</span>
<span id="cb57-15"><a href="systax.html#cb57-15"></a><span class="va">True</span></span></code></pre></div>
</div>
<div id="在字符串内查找和替换" class="section level3">
<h3><span class="header-section-number">2.6.10</span> 在字符串内查找和替换</h3>
<p>除了一般的序列操作，字符串还有独有的一些方法。如查找和替换：</p>
<div class="sourceCode" id="cb58"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb58-1"><a href="systax.html#cb58-1"></a><span class="bu">print</span>(s.find(<span class="st">&#39;in&#39;</span>))</span>
<span id="cb58-2"><a href="systax.html#cb58-2"></a><span class="bu">print</span>(s.replace(<span class="st">&#39;g&#39;</span>, <span class="st">&#39;gs&#39;</span>))  <span class="co"># 虽然显示字符串已被替换，但实际上是一个新的字符串。</span></span></code></pre></div>
<p>相关的方法见下表：</p>
<table>
<thead>
<tr class="header">
<th align="left">方法名称</th>
<th align="left">方法说明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">endswith(s1: str): bool</td>
<td align="left">如果字符串以指定的字符串结尾，则返回真</td>
</tr>
<tr class="even">
<td align="left">startswith(s1: str): bool</td>
<td align="left">如果字符串以指定的字符串开始，则返回真</td>
</tr>
<tr class="odd">
<td align="left">count(substring): int</td>
<td align="left">返回子字符串在字符串中出现的次数</td>
</tr>
<tr class="even">
<td align="left">find(s1): int</td>
<td align="left">返回子字符串在字符串中第一次出现的索引，如果没有，则返回-1</td>
</tr>
<tr class="odd">
<td align="left">rfind(s1): int</td>
<td align="left">返回子字符串在字符串中最后一次出现的索引，如果没有，则返回-1</td>
</tr>
</tbody>
</table>
<p>示例如下：</p>
<div class="sourceCode" id="cb59"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb59-1"><a href="systax.html#cb59-1"></a><span class="op">&gt;&gt;&gt;</span> s <span class="op">=</span> <span class="st">&quot;welcome to python&quot;</span></span>
<span id="cb59-2"><a href="systax.html#cb59-2"></a><span class="op">&gt;&gt;&gt;</span> s.endswith(<span class="st">&quot;thon&quot;</span>)</span>
<span id="cb59-3"><a href="systax.html#cb59-3"></a><span class="va">True</span></span>
<span id="cb59-4"><a href="systax.html#cb59-4"></a><span class="op">&gt;&gt;&gt;</span> s.startswith(<span class="st">&quot;good&quot;</span>)</span>
<span id="cb59-5"><a href="systax.html#cb59-5"></a><span class="va">False</span></span>
<span id="cb59-6"><a href="systax.html#cb59-6"></a><span class="op">&gt;&gt;&gt;</span> s.find(<span class="st">&quot;come&quot;</span>)</span>
<span id="cb59-7"><a href="systax.html#cb59-7"></a><span class="dv">3</span></span>
<span id="cb59-8"><a href="systax.html#cb59-8"></a><span class="op">&gt;&gt;&gt;</span> s.find(<span class="st">&quot;become&quot;</span>)</span>
<span id="cb59-9"><a href="systax.html#cb59-9"></a><span class="dv">-1</span></span>
<span id="cb59-10"><a href="systax.html#cb59-10"></a><span class="op">&gt;&gt;&gt;</span> s.rfind(<span class="st">&quot;o&quot;</span>)</span>
<span id="cb59-11"><a href="systax.html#cb59-11"></a><span class="dv">15</span></span>
<span id="cb59-12"><a href="systax.html#cb59-12"></a><span class="op">&gt;&gt;&gt;</span> s.count(<span class="st">&quot;o&quot;</span>)</span>
<span id="cb59-13"><a href="systax.html#cb59-13"></a><span class="dv">3</span></span></code></pre></div>

</div>
</div>
<div id="列表" class="section level2">
<h2><span class="header-section-number">2.7</span> 列表</h2>
<p>Python的列表（list）对象是最常用的序列(Sequence)。与字符串是不可变序列不同，列表是可变的。可通过对偏移量进行修改和读取。</p>
<div id="列表赋值" class="section level3">
<h3><span class="header-section-number">2.7.1</span> 列表赋值</h3>
<p>列表可通过索引对其对应的元素进行赋值，从而改变列表的内容，如：</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb60-1"><a href="systax.html#cb60-1"></a><span class="op">&gt;&gt;&gt;</span> a <span class="op">=</span> [<span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">2</span>]</span>
<span id="cb60-2"><a href="systax.html#cb60-2"></a><span class="op">&gt;&gt;&gt;</span> a[<span class="dv">1</span>] <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb60-3"><a href="systax.html#cb60-3"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(a)</span>
<span id="cb60-4"><a href="systax.html#cb60-4"></a>[<span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">2</span>]</span></code></pre></div>
<p>通过上述代码的运行，我们可以看到列表确实是可以改变的。</p>
</div>
<div id="删除元素" class="section level3">
<h3><span class="header-section-number">2.7.2</span> 删除元素</h3>
<p>使用del语句可以删除列表中的元素，如：</p>
<div class="sourceCode" id="cb61"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb61-1"><a href="systax.html#cb61-1"></a><span class="op">&gt;&gt;&gt;</span> a <span class="op">=</span> [<span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">2</span>]</span>
<span id="cb61-2"><a href="systax.html#cb61-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="kw">del</span> a[<span class="dv">1</span>]</span>
<span id="cb61-3"><a href="systax.html#cb61-3"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(a)</span>
<span id="cb61-4"><a href="systax.html#cb61-4"></a>[<span class="dv">2</span>, <span class="dv">2</span>]</span></code></pre></div>
</div>
<div id="分片赋值" class="section level3">
<h3><span class="header-section-number">2.7.3</span> 分片赋值</h3>
<p>分片赋值可以一次为多个元素赋值，并且不用考虑原列表的长度是否和新的列表长度一直，如：</p>
<div class="sourceCode" id="cb62"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb62-1"><a href="systax.html#cb62-1"></a><span class="op">&gt;&gt;&gt;</span> name <span class="op">=</span> <span class="bu">list</span>(<span class="st">&#39;Python&#39;</span>)</span>
<span id="cb62-2"><a href="systax.html#cb62-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(name)</span>
<span id="cb62-3"><a href="systax.html#cb62-3"></a>[<span class="st">&#39;P&#39;</span>, <span class="st">&#39;y&#39;</span>, <span class="st">&#39;t&#39;</span>, <span class="st">&#39;h&#39;</span>, <span class="st">&#39;o&#39;</span>, <span class="st">&#39;n&#39;</span>]</span>
<span id="cb62-4"><a href="systax.html#cb62-4"></a><span class="op">&gt;&gt;&gt;</span> name[<span class="dv">2</span>:] <span class="op">=</span> <span class="bu">list</span>(<span class="st">&#39;data&#39;</span>)</span>
<span id="cb62-5"><a href="systax.html#cb62-5"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(name)</span>
<span id="cb62-6"><a href="systax.html#cb62-6"></a>[<span class="st">&#39;P&#39;</span>, <span class="st">&#39;y&#39;</span>, <span class="st">&#39;d&#39;</span>, <span class="st">&#39;a&#39;</span>, <span class="st">&#39;t&#39;</span>, <span class="st">&#39;a&#39;</span>]</span></code></pre></div>
<p>上述代码中的list函数是Python内置函数，其作用是将字符串转换为列表。
运行结果显示，通过分片赋值，将原有列表[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]，修改为[‘P’, ‘y’, ‘d’, ‘a’, ‘t’, ‘a’]。</p>
<p>分片赋值还可以用来插入元素，如：</p>
<div class="sourceCode" id="cb63"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb63-1"><a href="systax.html#cb63-1"></a><span class="op">&gt;&gt;&gt;</span> name <span class="op">=</span> <span class="bu">list</span>(<span class="st">&#39;Python&#39;</span>)</span>
<span id="cb63-2"><a href="systax.html#cb63-2"></a><span class="op">&gt;&gt;&gt;</span> name[<span class="dv">1</span>:<span class="dv">1</span>] <span class="op">=</span> <span class="bu">list</span>(<span class="st">&#39;--&#39;</span>)</span>
<span id="cb63-3"><a href="systax.html#cb63-3"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(name)</span>
<span id="cb63-4"><a href="systax.html#cb63-4"></a>[<span class="st">&#39;P&#39;</span>, <span class="st">&#39;-&#39;</span>, <span class="st">&#39;-&#39;</span>, <span class="st">&#39;y&#39;</span>, <span class="st">&#39;t&#39;</span>, <span class="st">&#39;h&#39;</span>, <span class="st">&#39;o&#39;</span>, <span class="st">&#39;n&#39;</span>]</span></code></pre></div>
<p>结果显示将原有列表[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]，修改为[‘P’, ‘-’, ‘-’, ‘y’, ‘d’, ‘a’, ‘t’, ‘a’]。</p>
</div>
<div id="列表对象常用内置方法" class="section level3">
<h3><span class="header-section-number">2.7.4</span> 列表对象常用内置方法</h3>
<div id="追加列表元素" class="section level4">
<h4><span class="header-section-number">2.7.4.1</span> 追加列表元素</h4>
<p>。列表提供了在列表尾部追加新对象的方法append。</p>
<div class="sourceCode" id="cb64"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb64-1"><a href="systax.html#cb64-1"></a><span class="op">&gt;&gt;&gt;</span> code <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</span>
<span id="cb64-2"><a href="systax.html#cb64-2"></a><span class="op">&gt;&gt;&gt;</span> code.append(<span class="dv">4</span>)</span>
<span id="cb64-3"><a href="systax.html#cb64-3"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(code)</span>
<span id="cb64-4"><a href="systax.html#cb64-4"></a>[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]</span></code></pre></div>
</div>
<div id="计数" class="section level4">
<h4><span class="header-section-number">2.7.4.2</span> 计数</h4>
<p>count方法统计指定元素在列表中出现的次数，如：</p>
<div class="sourceCode" id="cb65"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb65-1"><a href="systax.html#cb65-1"></a><span class="op">&gt;&gt;&gt;</span> code <span class="op">=</span> [<span class="st">&#39;to&#39;</span>, <span class="st">&#39;be&#39;</span>, <span class="st">&#39;or&#39;</span>, <span class="st">&#39;not&#39;</span>, <span class="st">&#39;to&#39;</span>, <span class="st">&#39;be&#39;</span>]</span>
<span id="cb65-2"><a href="systax.html#cb65-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">print</span>(code.count(<span class="st">&#39;to&#39;</span>))</span>
<span id="cb65-3"><a href="systax.html#cb65-3"></a><span class="dv">2</span></span></code></pre></div>
<p>以上代码将统计出列表中‘to’元素出现的次数，结果为2。</p>
</div>
<div id="合并列表" class="section level4">
<h4><span class="header-section-number">2.7.4.3</span> 合并列表</h4>
<p>extend方法在列表的末尾一次性追加另一个序列中的多个值，如：</p>
<div class="sourceCode" id="cb66"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb66-1"><a href="systax.html#cb66-1"></a>a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</span>
<span id="cb66-2"><a href="systax.html#cb66-2"></a>b <span class="op">=</span> [<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>]</span>
<span id="cb66-3"><a href="systax.html#cb66-3"></a>a.extend(b)</span>
<span id="cb66-4"><a href="systax.html#cb66-4"></a><span class="bu">print</span>(a)</span></code></pre></div>
<p>以上代码将把b列表追加到a列表中，打印出的a列表的值为[1, 2, 3, 4, 5, 6]。和序列加运算不同，extend方法将改变原有列表的内容，而加运算却不会。例如：</p>
<div class="sourceCode" id="cb67"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb67-1"><a href="systax.html#cb67-1"></a>b <span class="op">=</span> [<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>]</span>
<span id="cb67-2"><a href="systax.html#cb67-2"></a>b <span class="op">+</span> [<span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>]</span>
<span id="cb67-3"><a href="systax.html#cb67-3"></a><span class="bu">print</span>(b)</span></code></pre></div>
<p>上述代码结果显示为[4, 5, 6]，b列表的内容并没有改变。</p>
</div>
<div id="元素索引" class="section level4">
<h4><span class="header-section-number">2.7.4.4</span> 元素索引</h4>
<p>index方法用于从列表中找出指定值第一次匹配的索引值。例如：</p>
<div class="sourceCode" id="cb68"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb68-1"><a href="systax.html#cb68-1"></a>a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>]</span>
<span id="cb68-2"><a href="systax.html#cb68-2"></a><span class="bu">print</span>(a.index(<span class="dv">1</span>))</span></code></pre></div>
<p>以上代码运行结果为0，即第一个1出现的索引为0。</p>
</div>
<div id="插入元素" class="section level4">
<h4><span class="header-section-number">2.7.4.5</span> 插入元素</h4>
<p>insert方法用于将对象插入到列表中，例如：</p>
<div class="sourceCode" id="cb69"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb69-1"><a href="systax.html#cb69-1"></a>a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</span>
<span id="cb69-2"><a href="systax.html#cb69-2"></a>a.insert(<span class="dv">2</span>, <span class="fl">2.5</span>)</span>
<span id="cb69-3"><a href="systax.html#cb69-3"></a><span class="bu">print</span>(a)</span></code></pre></div>
<p>运行结果为[1, 2, 2.5, 3]，insert方法的两个参数值很好理解，第一个参数为在哪个元素后插入，表示位置，第二个参数为插入的内容。</p>
</div>
<div id="pop" class="section level4">
<h4><span class="header-section-number">2.7.4.6</span> pop</h4>
<p>pop方法会移除列表中的一个元素，默认为最后一个，和append方法刚好相反，并且返回该元素的值。例如：</p>
<div class="sourceCode" id="cb70"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb70-1"><a href="systax.html#cb70-1"></a>a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</span>
<span id="cb70-2"><a href="systax.html#cb70-2"></a><span class="bu">print</span>(a.pop())</span>
<span id="cb70-3"><a href="systax.html#cb70-3"></a><span class="bu">print</span>(a)</span></code></pre></div>
<p>运行结果为3和[1, 2]，当然，pop方法也可以指定移除某个索引的元素。</p>
</div>
<div id="remove" class="section level4">
<h4><span class="header-section-number">2.7.4.7</span> remove</h4>
<p>remove方法用于移除列表中某个值的第一个匹配项：</p>
<div class="sourceCode" id="cb71"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb71-1"><a href="systax.html#cb71-1"></a>code <span class="op">=</span> [<span class="st">&#39;to&#39;</span>, <span class="st">&#39;be&#39;</span>, <span class="st">&#39;or&#39;</span>, <span class="st">&#39;not&#39;</span>, <span class="st">&#39;to&#39;</span>, <span class="st">&#39;be&#39;</span>]</span>
<span id="cb71-2"><a href="systax.html#cb71-2"></a><span class="bu">print</span>(code.remove(<span class="st">&#39;or&#39;</span>))</span>
<span id="cb71-3"><a href="systax.html#cb71-3"></a><span class="bu">print</span>(code)</span></code></pre></div>
<p>运行结果为None和[‘to’, ‘be’, ‘not’, ‘to’, ‘be’]。这说明remove方法并不返回匹配到的内容。</p>
</div>
<div id="reverse" class="section level4">
<h4><span class="header-section-number">2.7.4.8</span> reverse</h4>
<p>reverse方法将倒序排列列表元素：</p>
<div class="sourceCode" id="cb72"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb72-1"><a href="systax.html#cb72-1"></a>a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</span>
<span id="cb72-2"><a href="systax.html#cb72-2"></a>a.reverse()</span>
<span id="cb72-3"><a href="systax.html#cb72-3"></a><span class="bu">print</span>(a)</span></code></pre></div>
<p>运行结果为[3, 2, 1]。</p>
</div>
<div id="sort" class="section level4">
<h4><span class="header-section-number">2.7.4.9</span> sort</h4>
<p>sort方法用于对列表排序，如：</p>
<div class="sourceCode" id="cb73"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb73-1"><a href="systax.html#cb73-1"></a>a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">6</span>, <span class="dv">2</span>]</span>
<span id="cb73-2"><a href="systax.html#cb73-2"></a>a.sort()</span>
<span id="cb73-3"><a href="systax.html#cb73-3"></a><span class="bu">print</span>(a)</span></code></pre></div>
<p>运行结果为：[1, 2, 3, 4, 6, 8]。需要注意的是，sort方法没有返回值，并且改变列表的内容，如果你不但要排序，而且还要保持原有数据的内容，解决的方法之一是将原有内容赋值到另外一个变量中保存。</p>

</div>
</div>
</div>
<div id="字典" class="section level2">
<h2><span class="header-section-number">2.8</span> 字典</h2>
<p>字典（Dictionary）是Python中的一种数据类型，用来存储键（key）值（value）对。字典数据能够使用键名快速取回、添加、删除、编辑值。字典和其他语言中的数组（array）或者哈希表（hash）非常相似。字典是可变（mutable）序列。</p>
<div id="创建字典" class="section level3">
<h3><span class="header-section-number">2.8.1</span> 创建字典</h3>
<p>使用花括弧 <code>{}</code>就可创建字典。字典中的每一个项目都由键名、冒号<code>:</code>和值组成，多个项目之间用逗号<code>,</code>分割。让我们看一个实例：</p>
<div class="sourceCode" id="cb74"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb74-1"><a href="systax.html#cb74-1"></a>friends <span class="op">=</span> {</span>
<span id="cb74-2"><a href="systax.html#cb74-2"></a><span class="st">&#39;tom&#39;</span>   : <span class="st">&#39;66666666&#39;</span>,</span>
<span id="cb74-3"><a href="systax.html#cb74-3"></a><span class="st">&#39;jerry&#39;</span> : <span class="st">&#39;88888888&#39;</span></span>
<span id="cb74-4"><a href="systax.html#cb74-4"></a>}</span></code></pre></div>
<p>上面的变量friends是一个含有两个项目的字典。需要注意的一点是，键名必须是可哈希类型，而值可以是任意类型。字典中的键名必须是唯一的。</p>
</div>
<div id="获取修改和添加字典元素" class="section level3">
<h3><span class="header-section-number">2.8.2</span> 获取、修改和添加字典元素</h3>
<p>获取字典中的项目，使用如下语法：</p>
<div class="sourceCode" id="cb75"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb75-1"><a href="systax.html#cb75-1"></a>dictionary_name[<span class="st">&#39;key&#39;</span>]</span></code></pre></div>
<p>例如：</p>
<div class="sourceCode" id="cb76"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb76-1"><a href="systax.html#cb76-1"></a><span class="op">&gt;&gt;&gt;</span> friends[<span class="st">&#39;tom&#39;</span>]</span>
<span id="cb76-2"><a href="systax.html#cb76-2"></a><span class="co">&#39;66666666&#39;</span></span></code></pre></div>
<p>如果字典中存在指定的键名，则返回对应的值，否则抛出键名异常。</p>
<p>添加和编辑项目，使用如下语法：</p>
<div class="sourceCode" id="cb77"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb77-1"><a href="systax.html#cb77-1"></a>dictionary_name[<span class="st">&#39;newkey&#39;</span>] <span class="op">=</span> <span class="st">&#39;newvalue&#39;</span></span></code></pre></div>
<p>例如：</p>
<div class="sourceCode" id="cb78"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb78-1"><a href="systax.html#cb78-1"></a><span class="op">&gt;&gt;&gt;</span> friends[<span class="st">&#39;bob&#39;</span>] <span class="op">=</span> <span class="st">&#39;99999999&#39;</span></span>
<span id="cb78-2"><a href="systax.html#cb78-2"></a><span class="op">&gt;&gt;&gt;</span> friends</span>
<span id="cb78-3"><a href="systax.html#cb78-3"></a>{<span class="st">&#39;jerry&#39;</span>: <span class="st">&#39;88888888&#39;</span>, <span class="st">&#39;bob&#39;</span>: <span class="st">&#39;99999999&#39;</span>, <span class="st">&#39;tom&#39;</span>: <span class="st">&#39;66666666&#39;</span>}</span></code></pre></div>
<p>删除字典中的项目使用如下语法：</p>
<div class="sourceCode" id="cb79"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb79-1"><a href="systax.html#cb79-1"></a><span class="kw">del</span> dictionary_name[<span class="st">&#39;key&#39;</span>]</span></code></pre></div>
<p>例如：</p>
<div class="sourceCode" id="cb80"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb80-1"><a href="systax.html#cb80-1"></a><span class="op">&gt;&gt;&gt;</span>  <span class="kw">del</span> friends[<span class="st">&#39;bob&#39;</span>]</span>
<span id="cb80-2"><a href="systax.html#cb80-2"></a><span class="op">&gt;&gt;&gt;</span>  friends</span>
<span id="cb80-3"><a href="systax.html#cb80-3"></a>{<span class="st">&#39;tom&#39;</span>: <span class="st">&#39;66666666&#39;</span>, <span class="st">&#39;jerry&#39;</span>: <span class="st">&#39;88888888&#39;</span>}</span></code></pre></div>
</div>
<div id="遍历字典" class="section level3">
<h3><span class="header-section-number">2.8.3</span> 遍历字典</h3>
<p>我们可以使用循环遍历字典中的所有项目。</p>
<div class="sourceCode" id="cb81"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb81-1"><a href="systax.html#cb81-1"></a><span class="op">&gt;&gt;&gt;</span> friends <span class="op">=</span> {</span>
<span id="cb81-2"><a href="systax.html#cb81-2"></a>     <span class="st">&#39;tom&#39;</span>   : <span class="st">&#39;66666666&#39;</span>,</span>
<span id="cb81-3"><a href="systax.html#cb81-3"></a>     <span class="st">&#39;jerry&#39;</span>: <span class="st">&#39;88888888&#39;</span></span>
<span id="cb81-4"><a href="systax.html#cb81-4"></a> }</span>
<span id="cb81-5"><a href="systax.html#cb81-5"></a><span class="op">&gt;&gt;&gt;</span> <span class="cf">for</span> key <span class="kw">in</span> friends:</span>
<span id="cb81-6"><a href="systax.html#cb81-6"></a>     <span class="bu">print</span>(key, <span class="st">&quot;:&quot;</span>, friends[key])</span>
<span id="cb81-7"><a href="systax.html#cb81-7"></a></span>
<span id="cb81-8"><a href="systax.html#cb81-8"></a>tom : <span class="dv">66666666</span></span>
<span id="cb81-9"><a href="systax.html#cb81-9"></a>jerry : <span class="dv">88888888</span></span></code></pre></div>
<div id="字典比较" class="section level4">
<h4><span class="header-section-number">2.8.3.1</span> 字典比较</h4>
<p>使用 <code>==</code> 和 <code>!=</code> 操作符判断字典是否包含相同的项目。</p>
<div class="sourceCode" id="cb82"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb82-1"><a href="systax.html#cb82-1"></a><span class="op">&gt;&gt;&gt;</span> d1 <span class="op">=</span> {<span class="st">&quot;mike&quot;</span>:<span class="dv">41</span>, <span class="st">&quot;bob&quot;</span>:<span class="dv">3</span>}</span>
<span id="cb82-2"><a href="systax.html#cb82-2"></a><span class="op">&gt;&gt;&gt;</span> d2 <span class="op">=</span> {<span class="st">&quot;bob&quot;</span>:<span class="dv">3</span>, <span class="st">&quot;mike&quot;</span>:<span class="dv">41</span>}</span>
<span id="cb82-3"><a href="systax.html#cb82-3"></a><span class="op">&gt;&gt;&gt;</span> d1 <span class="op">==</span> d2</span>
<span id="cb82-4"><a href="systax.html#cb82-4"></a><span class="va">True</span></span>
<span id="cb82-5"><a href="systax.html#cb82-5"></a><span class="op">&gt;&gt;&gt;</span> d1 <span class="op">!=</span> d2</span>
<span id="cb82-6"><a href="systax.html#cb82-6"></a><span class="va">False</span></span>
<span id="cb82-7"><a href="systax.html#cb82-7"></a><span class="op">&gt;&gt;&gt;</span></span></code></pre></div>
<p>不能使用其它的关系操作符(<code>&lt;</code> , <code>&gt;</code> , <code>&gt;=</code> , <code>&lt;=</code> )比较字典类型变量。</p>
</div>
</div>
<div id="字典常用方法" class="section level3">
<h3><span class="header-section-number">2.8.4</span> 字典常用方法</h3>
<p>Python提供了多个内置的方法，用来操作字典，常用方法见下表：</p>
<table>
<thead>
<tr class="header">
<th align="left">方法名</th>
<th align="left">方法用途</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">popitem()</td>
<td align="left">返回并移除字典中的任意项目</td>
</tr>
<tr class="even">
<td align="left">clear()</td>
<td align="left">删除字典中的所有项目</td>
</tr>
<tr class="odd">
<td align="left">keys()</td>
<td align="left">以元组的形式获得字典的键名</td>
</tr>
<tr class="even">
<td align="left">values()</td>
<td align="left">以元组的形式获得字典的值</td>
</tr>
<tr class="odd">
<td align="left">get(key)</td>
<td align="left">获得指定键名对应的值</td>
</tr>
<tr class="even">
<td align="left">pop(key)</td>
<td align="left">移除指定键名的项目</td>
</tr>
</tbody>
</table>
<div class="sourceCode" id="cb83"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb83-1"><a href="systax.html#cb83-1"></a><span class="op">&gt;&gt;&gt;</span> friends <span class="op">=</span> {<span class="st">&#39;tom&#39;</span>: <span class="st">&#39;111-222-333&#39;</span>, <span class="st">&#39;bob&#39;</span>: <span class="st">&#39;888-999-666&#39;</span>, <span class="st">&#39;jerry&#39;</span>: <span class="st">&#39;666-33-111&#39;</span>}</span>
<span id="cb83-2"><a href="systax.html#cb83-2"></a></span>
<span id="cb83-3"><a href="systax.html#cb83-3"></a><span class="op">&gt;&gt;&gt;</span> friends.popitem()</span>
<span id="cb83-4"><a href="systax.html#cb83-4"></a>(<span class="st">&#39;tom&#39;</span>, <span class="st">&#39;111-222-333&#39;</span>)</span>
<span id="cb83-5"><a href="systax.html#cb83-5"></a></span>
<span id="cb83-6"><a href="systax.html#cb83-6"></a><span class="op">&gt;&gt;&gt;</span> friends.clear()</span>
<span id="cb83-7"><a href="systax.html#cb83-7"></a></span>
<span id="cb83-8"><a href="systax.html#cb83-8"></a><span class="op">&gt;&gt;&gt;</span>  friends</span>
<span id="cb83-9"><a href="systax.html#cb83-9"></a>{}</span>
<span id="cb83-10"><a href="systax.html#cb83-10"></a></span>
<span id="cb83-11"><a href="systax.html#cb83-11"></a><span class="op">&gt;&gt;&gt;</span> friends <span class="op">=</span> {<span class="st">&#39;tom&#39;</span>: <span class="st">&#39;111-222-333&#39;</span>, <span class="st">&#39;bob&#39;</span>: <span class="st">&#39;888-999-666&#39;</span>, <span class="st">&#39;jerry&#39;</span>: <span class="st">&#39;666-33-111&#39;</span>}</span>
<span id="cb83-12"><a href="systax.html#cb83-12"></a></span>
<span id="cb83-13"><a href="systax.html#cb83-13"></a><span class="op">&gt;&gt;&gt;</span> friends.keys()</span>
<span id="cb83-14"><a href="systax.html#cb83-14"></a>dict_keys([<span class="st">&#39;tom&#39;</span>, <span class="st">&#39;bob&#39;</span>, <span class="st">&#39;jerry&#39;</span>])</span>
<span id="cb83-15"><a href="systax.html#cb83-15"></a></span>
<span id="cb83-16"><a href="systax.html#cb83-16"></a><span class="op">&gt;&gt;&gt;</span> friends.values()</span>
<span id="cb83-17"><a href="systax.html#cb83-17"></a>dict_values([<span class="st">&#39;111-222-333&#39;</span>, <span class="st">&#39;888-999-666&#39;</span>, <span class="st">&#39;666-33-111&#39;</span>])</span>
<span id="cb83-18"><a href="systax.html#cb83-18"></a></span>
<span id="cb83-19"><a href="systax.html#cb83-19"></a><span class="op">&gt;&gt;&gt;</span> friends.get(<span class="st">&#39;tom&#39;</span>)</span>
<span id="cb83-20"><a href="systax.html#cb83-20"></a><span class="co">&#39;111-222-333&#39;</span></span>
<span id="cb83-21"><a href="systax.html#cb83-21"></a></span>
<span id="cb83-22"><a href="systax.html#cb83-22"></a><span class="op">&gt;&gt;&gt;</span> friends.get(<span class="st">&#39;mike&#39;</span>, <span class="st">&#39;Not Exists&#39;</span>)</span>
<span id="cb83-23"><a href="systax.html#cb83-23"></a><span class="co">&#39;Not Exists&#39;</span></span>
<span id="cb83-24"><a href="systax.html#cb83-24"></a></span>
<span id="cb83-25"><a href="systax.html#cb83-25"></a><span class="op">&gt;&gt;&gt;</span> friends.pop(<span class="st">&#39;bob&#39;</span>)</span>
<span id="cb83-26"><a href="systax.html#cb83-26"></a><span class="co">&#39;888-999-666&#39;</span></span>
<span id="cb83-27"><a href="systax.html#cb83-27"></a></span>
<span id="cb83-28"><a href="systax.html#cb83-28"></a><span class="op">&gt;&gt;&gt;</span> friends</span>
<span id="cb83-29"><a href="systax.html#cb83-29"></a>{<span class="st">&#39;tom&#39;</span>: <span class="st">&#39;111-222-333&#39;</span>, <span class="st">&#39;jerry&#39;</span>: <span class="st">&#39;666-33-111&#39;</span>}</span></code></pre></div>
</div>
<div id="字典的排序" class="section level3">
<h3><span class="header-section-number">2.8.5</span> 字典的排序</h3>
<p>字典的排序，可以使用sorted()函数，语法如下：</p>
<div class="sourceCode" id="cb84"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb84-1"><a href="systax.html#cb84-1"></a><span class="bu">sorted</span>(iterable,key,reverse)</span></code></pre></div>
<ol style="list-style-type: decimal">
<li><code>iterable</code>表示可以迭代的对象，例如可以是<code>dict.items()</code>、<code>dict.keys()</code>等;</li>
<li><code>key</code>是一个函数，用来选取参与比较的元素;</li>
<li><code>reverse</code>则是用来指定排序是倒序还是顺序，reverse=true则是倒序，reverse=false时则是顺序，默认时reverse=false。</li>
</ol>
<div id="sorted函数按key值对字典排序" class="section level4">
<h4><span class="header-section-number">2.8.5.1</span> sorted函数按key值对字典排序</h4>
<p>直接使用<code>sorted(d.keys())</code>就能按key值对字典排序，这里是按照顺序对<code>key</code>值排序的，如果想按照倒序排序的话，则只要将<code>reverse</code>置为<code>true</code>即可。</p>
<div class="sourceCode" id="cb85"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb85-1"><a href="systax.html#cb85-1"></a><span class="op">&gt;&gt;&gt;</span> dd <span class="op">=</span> {<span class="st">&#39;borisakunin&#39;</span>: <span class="dv">4691</span>, <span class="st">&#39;doctor_liza&#39;</span>: <span class="dv">3046</span>, <span class="st">&#39;tareeva&#39;</span>: <span class="dv">2970</span>, <span class="st">&#39;cheger&#39;</span>: <span class="dv">2887</span>, <span class="st">&#39;karial&#39;</span>: <span class="dv">2717</span>, <span class="st">&#39;snorapp&#39;</span>: <span class="dv">2707</span>, <span class="st">&#39;masha_koroleva&#39;</span>: <span class="dv">2683</span>, <span class="st">&#39;elladkin&#39;</span>: <span class="dv">2616</span>, <span class="st">&#39;samoleg&#39;</span>: <span class="dv">2597</span>}</span>
<span id="cb85-2"><a href="systax.html#cb85-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">sorted</span>(dd.keys())</span>
<span id="cb85-3"><a href="systax.html#cb85-3"></a>[<span class="st">&#39;borisakunin&#39;</span>, <span class="st">&#39;cheger&#39;</span>, <span class="st">&#39;doctor_liza&#39;</span>, <span class="st">&#39;elladkin&#39;</span>, <span class="st">&#39;karial&#39;</span>, <span class="st">&#39;masha_koroleva&#39;</span>, <span class="st">&#39;samoleg&#39;</span>, <span class="st">&#39;snorapp&#39;</span>, <span class="st">&#39;tareeva&#39;</span>]</span>
<span id="cb85-4"><a href="systax.html#cb85-4"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">sorted</span>(dd.keys(),reverse<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb85-5"><a href="systax.html#cb85-5"></a>[<span class="st">&#39;tareeva&#39;</span>, <span class="st">&#39;snorapp&#39;</span>, <span class="st">&#39;samoleg&#39;</span>, <span class="st">&#39;masha_koroleva&#39;</span>, <span class="st">&#39;karial&#39;</span>, <span class="st">&#39;elladkin&#39;</span>, <span class="st">&#39;doctor_liza&#39;</span>, <span class="st">&#39;cheger&#39;</span>, <span class="st">&#39;borisakunin&#39;</span>]</span></code></pre></div>
</div>
<div id="sorted函数按value值对字典排序" class="section level4">
<h4><span class="header-section-number">2.8.5.2</span> sorted函数按value值对字典排序</h4>
<p>要对字典的value排序则需要用到key参数，常使用lambda表达式的方法，如下：</p>
<div class="sourceCode" id="cb86"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb86-1"><a href="systax.html#cb86-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">sorted</span>(dd.items(),key<span class="op">=</span><span class="kw">lambda</span> item:item[<span class="dv">1</span>])</span>
<span id="cb86-2"><a href="systax.html#cb86-2"></a>[(<span class="st">&#39;samoleg&#39;</span>, <span class="dv">2597</span>), (<span class="st">&#39;elladkin&#39;</span>, <span class="dv">2616</span>), (<span class="st">&#39;masha_koroleva&#39;</span>, <span class="dv">2683</span>), (<span class="st">&#39;snorapp&#39;</span>, <span class="dv">2707</span>), (<span class="st">&#39;karial&#39;</span>, <span class="dv">2717</span>), (<span class="st">&#39;cheger&#39;</span>, <span class="dv">2887</span>), (<span class="st">&#39;tareeva&#39;</span>, <span class="dv">2970</span>), (<span class="st">&#39;doctor_liza&#39;</span>, <span class="dv">3046</span>), (<span class="st">&#39;borisakunin&#39;</span>, <span class="dv">4691</span>)]</span>
<span id="cb86-3"><a href="systax.html#cb86-3"></a></span>
<span id="cb86-4"><a href="systax.html#cb86-4"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">sorted</span>(dd.items(),key<span class="op">=</span><span class="kw">lambda</span> item:item[<span class="dv">1</span>],reverse<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb86-5"><a href="systax.html#cb86-5"></a>[(<span class="st">&#39;borisakunin&#39;</span>, <span class="dv">4691</span>), (<span class="st">&#39;doctor_liza&#39;</span>, <span class="dv">3046</span>), (<span class="st">&#39;tareeva&#39;</span>, <span class="dv">2970</span>), (<span class="st">&#39;cheger&#39;</span>, <span class="dv">2887</span>), (<span class="st">&#39;karial&#39;</span>, <span class="dv">2717</span>), (<span class="st">&#39;snorapp&#39;</span>, <span class="dv">2707</span>), (<span class="st">&#39;masha_koroleva&#39;</span>, <span class="dv">2683</span>), (<span class="st">&#39;elladkin&#39;</span>, <span class="dv">2616</span>), (<span class="st">&#39;samoleg&#39;</span>, <span class="dv">2597</span>)]</span></code></pre></div>
<p>这里的<code>dd.items()</code>实际上是将<code>dd</code>转换为可迭代对象，<code>items()</code>方法将字典的元素转化为了元组，而这里<code>key</code>参数对应的<code>lambda</code>表达式的意思则是选取元组中的第二个元素作为比较对象，如果写作<code>key=lambda item:item[0]</code>的话则是选取第一个元素作为比较对象，也就是<code>key</code>值作为比较对象。</p>
<p>注意排序函数<code>sorted()</code>返回值是一个<code>list</code>，而原字典中的名值对被转换为了<code>list</code>中的元组。</p>

</div>
</div>
</div>
<div id="元组" class="section level2">
<h2><span class="header-section-number">2.9</span> 元组</h2>
<p>在Python中，元组（Tuple）和列表非常相似，与列表不同的是，元组一旦创立，就不可改变，也就是说，元组是不可变的。</p>
<div id="创建元组" class="section level3">
<h3><span class="header-section-number">2.9.1</span> 创建元组</h3>
<div class="sourceCode" id="cb87"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb87-1"><a href="systax.html#cb87-1"></a><span class="op">&gt;&gt;&gt;</span> t1 <span class="op">=</span> ()                 <span class="co"># 创建一个空元组</span></span>
<span id="cb87-2"><a href="systax.html#cb87-2"></a><span class="op">&gt;&gt;&gt;</span> t2 <span class="op">=</span> (<span class="dv">11</span>,<span class="dv">22</span>,<span class="dv">33</span>)         <span class="co"># 创建一个包含三个元素的元组</span></span>
<span id="cb87-3"><a href="systax.html#cb87-3"></a><span class="op">&gt;&gt;&gt;</span> t3 <span class="op">=</span> <span class="bu">tuple</span>([<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>])   <span class="co"># 使用列表创建元组</span></span>
<span id="cb87-4"><a href="systax.html#cb87-4"></a><span class="op">&gt;&gt;&gt;</span> t4 <span class="op">=</span> <span class="bu">tuple</span>(<span class="st">&quot;abc&quot;</span>)       <span class="co"># 使用字符串创建元组</span></span></code></pre></div>
</div>
<div id="元组相关方法" class="section level3">
<h3><span class="header-section-number">2.9.2</span> 元组相关方法</h3>
<p>元组也是序列，因此序列能使用的方法，如max , min , len , sum方法元组也能使用。</p>
<div class="sourceCode" id="cb88"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb88-1"><a href="systax.html#cb88-1"></a><span class="op">&gt;&gt;&gt;</span> t1 <span class="op">=</span> (<span class="dv">1</span>, <span class="dv">12</span>, <span class="dv">55</span>, <span class="dv">12</span>, <span class="dv">81</span>)</span>
<span id="cb88-2"><a href="systax.html#cb88-2"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">min</span>(t1)</span>
<span id="cb88-3"><a href="systax.html#cb88-3"></a><span class="dv">1</span></span>
<span id="cb88-4"><a href="systax.html#cb88-4"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">max</span>(t1)</span>
<span id="cb88-5"><a href="systax.html#cb88-5"></a><span class="dv">81</span></span>
<span id="cb88-6"><a href="systax.html#cb88-6"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">sum</span>(t1)</span>
<span id="cb88-7"><a href="systax.html#cb88-7"></a><span class="dv">161</span></span>
<span id="cb88-8"><a href="systax.html#cb88-8"></a><span class="op">&gt;&gt;&gt;</span> <span class="bu">len</span>(t1)</span>
<span id="cb88-9"><a href="systax.html#cb88-9"></a><span class="dv">5</span></span></code></pre></div>

</div>
</div>
<div id="控制声明" class="section level2">
<h2><span class="header-section-number">2.10</span> 控制声明</h2>
<p>在程序中，常常要根据一些条件执行相应的命令。</p>
<div id="分支判断" class="section level3">
<h3><span class="header-section-number">2.10.1</span> 分支判断</h3>
<p>Python 使用<code>if-else</code>进行控制声明。语法如下：</p>
<div class="sourceCode" id="cb89"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb89-1"><a href="systax.html#cb89-1"></a><span class="cf">if</span> boolean<span class="op">-</span>expression:</span>
<span id="cb89-2"><a href="systax.html#cb89-2"></a>   <span class="co">#statements</span></span>
<span id="cb89-3"><a href="systax.html#cb89-3"></a><span class="cf">else</span>:</span>
<span id="cb89-4"><a href="systax.html#cb89-4"></a>   <span class="co">#statements</span></span></code></pre></div>
<p>在每一个 if 程序块中，必须使用相同数量的缩进，否则会产生语法错误。这是 Python 和其他语言非常不同的一点。</p>
<p>现在我们看一个例子：</p>
<div class="sourceCode" id="cb90"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb90-1"><a href="systax.html#cb90-1"></a>i <span class="op">=</span> <span class="dv">11</span></span>
<span id="cb90-2"><a href="systax.html#cb90-2"></a><span class="cf">if</span> i <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb90-3"><a href="systax.html#cb90-3"></a>    <span class="bu">print</span>(<span class="st">&quot;偶数&quot;</span>)</span>
<span id="cb90-4"><a href="systax.html#cb90-4"></a><span class="cf">else</span>:</span>
<span id="cb90-5"><a href="systax.html#cb90-5"></a>    <span class="bu">print</span>(<span class="st">&quot;奇数&quot;</span>)</span></code></pre></div>
<p>运行结果将根据 i 的值发生变化。</p>
<p>如果需要判断多个条件，我们就可以使用 if-elif-else 控制声明，例如：</p>
<div class="sourceCode" id="cb91"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb91-1"><a href="systax.html#cb91-1"></a>today <span class="op">=</span> <span class="st">&quot;monday&quot;</span></span>
<span id="cb91-2"><a href="systax.html#cb91-2"></a></span>
<span id="cb91-3"><a href="systax.html#cb91-3"></a><span class="cf">if</span> today <span class="op">==</span> <span class="st">&quot;monday&quot;</span>:</span>
<span id="cb91-4"><a href="systax.html#cb91-4"></a>   <span class="bu">print</span>(<span class="st">&quot;this is monday&quot;</span>)</span>
<span id="cb91-5"><a href="systax.html#cb91-5"></a><span class="cf">elif</span> today <span class="op">==</span> <span class="st">&quot;tuesday&quot;</span>:</span>
<span id="cb91-6"><a href="systax.html#cb91-6"></a>   <span class="bu">print</span>(<span class="st">&quot;this is tuesday&quot;</span>)</span>
<span id="cb91-7"><a href="systax.html#cb91-7"></a><span class="cf">elif</span> today <span class="op">==</span> <span class="st">&quot;wednesday&quot;</span>:</span>
<span id="cb91-8"><a href="systax.html#cb91-8"></a>   <span class="bu">print</span>(<span class="st">&quot;this is wednesday&quot;</span>)</span>
<span id="cb91-9"><a href="systax.html#cb91-9"></a><span class="cf">elif</span> today <span class="op">==</span> <span class="st">&quot;thursday&quot;</span>:</span>
<span id="cb91-10"><a href="systax.html#cb91-10"></a>   <span class="bu">print</span>(<span class="st">&quot;this is thursday&quot;</span>)</span>
<span id="cb91-11"><a href="systax.html#cb91-11"></a><span class="cf">elif</span> today <span class="op">==</span> <span class="st">&quot;friday&quot;</span>:</span>
<span id="cb91-12"><a href="systax.html#cb91-12"></a>   <span class="bu">print</span>(<span class="st">&quot;this is friday&quot;</span>)</span>
<span id="cb91-13"><a href="systax.html#cb91-13"></a><span class="cf">elif</span> today <span class="op">==</span> <span class="st">&quot;saturday&quot;</span>:</span>
<span id="cb91-14"><a href="systax.html#cb91-14"></a>   <span class="bu">print</span>(<span class="st">&quot;this is saturday&quot;</span>)</span>
<span id="cb91-15"><a href="systax.html#cb91-15"></a><span class="cf">elif</span> today <span class="op">==</span> <span class="st">&quot;sunday&quot;</span>:</span>
<span id="cb91-16"><a href="systax.html#cb91-16"></a>   <span class="bu">print</span>(<span class="st">&quot;this is sunday&quot;</span>)</span>
<span id="cb91-17"><a href="systax.html#cb91-17"></a><span class="cf">else</span>:</span>
<span id="cb91-18"><a href="systax.html#cb91-18"></a>   <span class="bu">print</span>(<span class="st">&quot;something else&quot;</span>)</span></code></pre></div>
<p>我们可以根据实际需求，添加对应的多个 elif 条件。</p>
</div>
<div id="分支嵌套" class="section level3">
<h3><span class="header-section-number">2.10.2</span> 分支嵌套</h3>
<p>我们可以在 if 声明语句块中嵌套使用 if 声明。例如：</p>
<div class="sourceCode" id="cb92"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb92-1"><a href="systax.html#cb92-1"></a>today <span class="op">=</span> <span class="st">&quot;holiday&quot;</span></span>
<span id="cb92-2"><a href="systax.html#cb92-2"></a>bank_balance <span class="op">=</span> <span class="dv">25000</span></span>
<span id="cb92-3"><a href="systax.html#cb92-3"></a><span class="cf">if</span> today <span class="op">==</span> <span class="st">&quot;holiday&quot;</span>:</span>
<span id="cb92-4"><a href="systax.html#cb92-4"></a>    <span class="cf">if</span> bank_balance <span class="op">&gt;</span> <span class="dv">20000</span>:</span>
<span id="cb92-5"><a href="systax.html#cb92-5"></a>        <span class="bu">print</span>(<span class="st">&quot;Go for shopping&quot;</span>)</span>
<span id="cb92-6"><a href="systax.html#cb92-6"></a>    <span class="cf">else</span>:</span>
<span id="cb92-7"><a href="systax.html#cb92-7"></a>        <span class="bu">print</span>(<span class="st">&quot;Watch TV&quot;</span>)</span>
<span id="cb92-8"><a href="systax.html#cb92-8"></a><span class="cf">else</span>:</span>
<span id="cb92-9"><a href="systax.html#cb92-9"></a>    <span class="bu">print</span>(<span class="st">&quot;normal working day&quot;</span>)</span></code></pre></div>
</div>
<div id="三元运算符" class="section level3">
<h3><span class="header-section-number">2.10.3</span> 三元运算符</h3>
<p>在其他语言中，有类似<code>condition？true：false</code>的三元运算符，在 Python 中，可以这样实现：</p>
<div class="sourceCode" id="cb93"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb93-1"><a href="systax.html#cb93-1"></a>true <span class="cf">if</span> condition <span class="cf">else</span> false</span></code></pre></div>
<p>例如：</p>
<div class="sourceCode" id="cb94"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb94-1"><a href="systax.html#cb94-1"></a><span class="kw">def</span> b(a):</span>
<span id="cb94-2"><a href="systax.html#cb94-2"></a>    <span class="cf">return</span> a<span class="op">+</span><span class="dv">2</span> <span class="cf">if</span> a <span class="op">&gt;</span> <span class="dv">10</span> <span class="cf">else</span> <span class="dv">5</span></span>
<span id="cb94-3"><a href="systax.html#cb94-3"></a></span>
<span id="cb94-4"><a href="systax.html#cb94-4"></a><span class="bu">print</span>(b(<span class="dv">11</span>), b(<span class="dv">4</span>))</span></code></pre></div>
<p>上面的代码将输出<code>13 6</code>。</p>

</div>
</div>
<div id="循环" class="section level2">
<h2><span class="header-section-number">2.11</span> 循环</h2>
<p>Python 只有两种循环：for 循环和 while 循环。</p>
<div id="for-循环" class="section level3">
<h3><span class="header-section-number">2.11.1</span> for 循环</h3>
<p>for 循环语法：</p>
<div class="sourceCode" id="cb95"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb95-1"><a href="systax.html#cb95-1"></a><span class="cf">for</span> i <span class="kw">in</span> iterable_object:</span>
<span id="cb95-2"><a href="systax.html#cb95-2"></a>   <span class="co"># do something</span></span></code></pre></div>
<p>所有在 for 循环或者 while 循环中的声明，必须使用相同的缩进值。否则会出现语法错误。</p>
<p>我们看下面这段代码：</p>
<div class="sourceCode" id="cb96"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb96-1"><a href="systax.html#cb96-1"></a>mylist <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]</span>
<span id="cb96-2"><a href="systax.html#cb96-2"></a></span>
<span id="cb96-3"><a href="systax.html#cb96-3"></a><span class="cf">for</span> i <span class="kw">in</span> mylist:</span>
<span id="cb96-4"><a href="systax.html#cb96-4"></a>    <span class="bu">print</span>(i)</span></code></pre></div>
<p>在第一次循环时，值 1 被传递给 i，第二次循环时，值 2 被传递给 i。循环一直到列表变量 mylist 没有更多元素时停止。运行结果为：</p>
<div class="sourceCode" id="cb97"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb97-1"><a href="systax.html#cb97-1"></a><span class="dv">1</span></span>
<span id="cb97-2"><a href="systax.html#cb97-2"></a><span class="dv">2</span></span>
<span id="cb97-3"><a href="systax.html#cb97-3"></a><span class="dv">3</span></span>
<span id="cb97-4"><a href="systax.html#cb97-4"></a><span class="dv">4</span></span></code></pre></div>
</div>
<div id="范围循环" class="section level3">
<h3><span class="header-section-number">2.11.2</span> 范围循环</h3>
<p>range() 函数能够指定循环的起始值和结束值，从而让循环体在指定的范围内循环。例如：</p>
<div class="sourceCode" id="cb98"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb98-1"><a href="systax.html#cb98-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">10</span>):</span>
<span id="cb98-2"><a href="systax.html#cb98-2"></a>    <span class="bu">print</span>(i)                    <span class="co"># 0-9</span></span>
<span id="cb98-3"><a href="systax.html#cb98-3"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">10</span>):</span>
<span id="cb98-4"><a href="systax.html#cb98-4"></a>    <span class="bu">print</span>(i)                    <span class="co"># 1-9</span></span>
<span id="cb98-5"><a href="systax.html#cb98-5"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">10</span>,<span class="dv">2</span>):</span>
<span id="cb98-6"><a href="systax.html#cb98-6"></a>    <span class="bu">print</span>(i)                    <span class="co"># 1,3,5,7</span></span></code></pre></div>
<p>range() 函数只有 1 个参数时，表示从 0 开始循环；两个参数时，第一个参数是起始值，第二个参数是结束值；三个参数时，第三个参数表示循环步长。</p>
</div>
<div id="while-循环" class="section level3">
<h3><span class="header-section-number">2.11.3</span> while 循环</h3>
<p>语法：</p>
<div class="sourceCode" id="cb99"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb99-1"><a href="systax.html#cb99-1"></a><span class="cf">while</span> condition:</span>
<span id="cb99-2"><a href="systax.html#cb99-2"></a>    <span class="co"># do something</span></span></code></pre></div>
<p>While 循环会一直执行循环体内部的声明，直到条件变成 false。每次循环都会检查判断条件，如果为真，就继续循环。例如：</p>
<div class="sourceCode" id="cb100"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb100-1"><a href="systax.html#cb100-1"></a>count <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb100-2"><a href="systax.html#cb100-2"></a></span>
<span id="cb100-3"><a href="systax.html#cb100-3"></a><span class="cf">while</span> count <span class="op">&lt;</span> <span class="dv">10</span>:</span>
<span id="cb100-4"><a href="systax.html#cb100-4"></a>    <span class="bu">print</span>(count)</span>
<span id="cb100-5"><a href="systax.html#cb100-5"></a>    count <span class="op">+=</span> <span class="dv">1</span></span></code></pre></div>
<p>这段代码将会打印出 0-9，直到 count 等于 10。</p>
</div>
<div id="中断循环" class="section level3">
<h3><span class="header-section-number">2.11.4</span> 中断循环</h3>
<p>使用 break 语句，可以中断循环，例如：</p>
<div class="sourceCode" id="cb101"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb101-1"><a href="systax.html#cb101-1"></a>count <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb101-2"><a href="systax.html#cb101-2"></a></span>
<span id="cb101-3"><a href="systax.html#cb101-3"></a><span class="cf">while</span> count <span class="op">&lt;</span> <span class="dv">10</span>:</span>
<span id="cb101-4"><a href="systax.html#cb101-4"></a>    count <span class="op">+=</span> <span class="dv">1</span></span>
<span id="cb101-5"><a href="systax.html#cb101-5"></a>    <span class="cf">if</span> count <span class="op">==</span> <span class="dv">5</span>:</span>
<span id="cb101-6"><a href="systax.html#cb101-6"></a>        <span class="cf">break</span></span>
<span id="cb101-7"><a href="systax.html#cb101-7"></a>    <span class="bu">print</span>(<span class="st">&quot;inside loop&quot;</span>, count)</span>
<span id="cb101-8"><a href="systax.html#cb101-8"></a></span>
<span id="cb101-9"><a href="systax.html#cb101-9"></a><span class="bu">print</span>(<span class="st">&quot;out of while loop&quot;</span>)</span></code></pre></div>
<p>运行结果为：</p>
<div class="sourceCode" id="cb102"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb102-1"><a href="systax.html#cb102-1"></a>inside loop <span class="dv">1</span></span>
<span id="cb102-2"><a href="systax.html#cb102-2"></a>inside loop <span class="dv">2</span></span>
<span id="cb102-3"><a href="systax.html#cb102-3"></a>inside loop <span class="dv">3</span></span>
<span id="cb102-4"><a href="systax.html#cb102-4"></a>inside loop <span class="dv">4</span></span>
<span id="cb102-5"><a href="systax.html#cb102-5"></a>out of <span class="cf">while</span> loop</span></code></pre></div>
</div>
<div id="继续循环" class="section level3">
<h3><span class="header-section-number">2.11.5</span> 继续循环</h3>
<p>当循环体内部出现 continue 声明时，会结束本次循环，跳转到循环体开始位置，开始下一次循环。例如：</p>
<div class="sourceCode" id="cb103"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb103-1"><a href="systax.html#cb103-1"></a>count <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb103-2"><a href="systax.html#cb103-2"></a></span>
<span id="cb103-3"><a href="systax.html#cb103-3"></a><span class="cf">while</span> count <span class="op">&lt;</span> <span class="dv">10</span>:</span>
<span id="cb103-4"><a href="systax.html#cb103-4"></a>    count <span class="op">+=</span> <span class="dv">1</span></span>
<span id="cb103-5"><a href="systax.html#cb103-5"></a>    <span class="cf">if</span> count <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb103-6"><a href="systax.html#cb103-6"></a>        <span class="cf">continue</span></span>
<span id="cb103-7"><a href="systax.html#cb103-7"></a>    <span class="bu">print</span>(count)</span></code></pre></div>
<p>运行结果将打印出 1，3，5，7，9。</p>

</div>
</div>
<div id="函数" class="section level2">
<h2><span class="header-section-number">2.12</span> 函数</h2>
<p>函数是可重用的代码块，使用函数可以帮助我们组织代码的结构。我们创建函数的目的，是能在程序运行中多次使用一系列代码，而不用重复书写代码。</p>
<div id="创建函数" class="section level3">
<h3><span class="header-section-number">2.12.1</span> 创建函数</h3>
<p>Python使用def关键词创建函数，语法如下：</p>
<div class="sourceCode" id="cb104"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb104-1"><a href="systax.html#cb104-1"></a><span class="kw">def</span> function_name(arg1, arg2, arg3, .... argN):</span>
<span id="cb104-2"><a href="systax.html#cb104-2"></a>     <span class="co">#statement inside function</span></span></code></pre></div>
<p>空白区在 Python 中十分重要。实际上，空白区在各行的开头非常重要。这被称作缩进（Indentation）。在逻辑行的开头留下空白区（使用空格或制表符）用以确定各逻辑行的缩进级别，而后者又可用于确定语句的分组。</p>
<p>这意味着放置在一起的语句必须拥有相同的缩进。每一组这样的语句被称为块（block）。有一件事你需要记住：错误的缩进可能会导致错误。</p>
<p>所有在函数内部的声明，都必须使用相等的缩进。函数可以没有参数，也可以有多个参数。多个参数之间用逗号隔开。还可以使用pass关键字忽略掉函数主题的声明。</p>
<p>我们看一个函数的例子，下面的函数将计算指定范围的整数之和：</p>
<div class="sourceCode" id="cb105"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb105-1"><a href="systax.html#cb105-1"></a><span class="kw">def</span> <span class="bu">sum</span>(start, end):</span>
<span id="cb105-2"><a href="systax.html#cb105-2"></a>    result <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb105-3"><a href="systax.html#cb105-3"></a>    <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(start, end <span class="op">+</span> <span class="dv">1</span>):</span>
<span id="cb105-4"><a href="systax.html#cb105-4"></a>        result <span class="op">+=</span> i</span>
<span id="cb105-5"><a href="systax.html#cb105-5"></a>    <span class="bu">print</span>(result)</span>
<span id="cb105-6"><a href="systax.html#cb105-6"></a></span>
<span id="cb105-7"><a href="systax.html#cb105-7"></a><span class="bu">sum</span>(<span class="dv">1</span>, <span class="dv">10</span>)</span></code></pre></div>
<p>在上面的代码中，我们定义了一个叫作<code>sum()</code>的函数，该函数有两个参数（<code>start</code>和<code>end</code>），该函数将从<code>start</code>开始，累加到<code>end</code>，最后打印出累积之和。代码运行的结果为55。</p>
</div>
<div id="函数返回值" class="section level3">
<h3><span class="header-section-number">2.12.2</span> 函数返回值</h3>
<p>上文定义的函数只是简单地在控制台打印出结果，如果我们想要将计算结果赋值给变量，以便做更深入的处理时应该怎么办？当我们遇到这种情况时，可使用<code>return</code>语句，将返回函数计算结果并且退出函数。例如：</p>
<div class="sourceCode" id="cb106"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb106-1"><a href="systax.html#cb106-1"></a><span class="kw">def</span> sumReturn(start, end):</span>
<span id="cb106-2"><a href="systax.html#cb106-2"></a>    result <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb106-3"><a href="systax.html#cb106-3"></a>    <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(start, end <span class="op">+</span> <span class="dv">1</span>):</span>
<span id="cb106-4"><a href="systax.html#cb106-4"></a>        result <span class="op">+=</span> i</span>
<span id="cb106-5"><a href="systax.html#cb106-5"></a>    <span class="cf">return</span> result</span>
<span id="cb106-6"><a href="systax.html#cb106-6"></a></span>
<span id="cb106-7"><a href="systax.html#cb106-7"></a>a <span class="op">=</span> sumReturn(<span class="dv">1</span>, <span class="dv">5</span>)</span>
<span id="cb106-8"><a href="systax.html#cb106-8"></a><span class="bu">print</span>(a)</span></code></pre></div>
<p>在上面这段代码中，我们定义了有返回值的函数<code>sumReturn()</code>，并将其结果赋值给变量<code>a</code>。上面代码的运行结果为15。</p>
<p>当然，return语句也可以不返回值，而是用来退出函数（实际上会返回None，为NoneType对象）。每一个函数都在其末尾隐含了一句 return None，除非你写了你自己的 return 语句。</p>
<div class="sourceCode" id="cb107"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb107-1"><a href="systax.html#cb107-1"></a><span class="kw">def</span> sum2(start, end):</span>
<span id="cb107-2"><a href="systax.html#cb107-2"></a>    <span class="cf">if</span>(start <span class="op">&gt;</span> end):</span>
<span id="cb107-3"><a href="systax.html#cb107-3"></a>        <span class="bu">print</span>(<span class="st">&quot;start should be less than end&quot;</span>)</span>
<span id="cb107-4"><a href="systax.html#cb107-4"></a>        <span class="cf">return</span></span>
<span id="cb107-5"><a href="systax.html#cb107-5"></a>    result <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb107-6"><a href="systax.html#cb107-6"></a>    <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(start, end <span class="op">+</span> <span class="dv">1</span>):</span>
<span id="cb107-7"><a href="systax.html#cb107-7"></a>        result <span class="op">+=</span> i</span>
<span id="cb107-8"><a href="systax.html#cb107-8"></a>    <span class="cf">return</span> result</span>
<span id="cb107-9"><a href="systax.html#cb107-9"></a></span>
<span id="cb107-10"><a href="systax.html#cb107-10"></a>s <span class="op">=</span> sum2(<span class="dv">110</span>, <span class="dv">50</span>)</span>
<span id="cb107-11"><a href="systax.html#cb107-11"></a><span class="bu">print</span>(s, <span class="bu">type</span>(s))</span></code></pre></div>
<p>上述代码的运行结果如下：</p>
<div class="sourceCode" id="cb108"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb108-1"><a href="systax.html#cb108-1"></a>start should be less than end</span>
<span id="cb108-2"><a href="systax.html#cb108-2"></a><span class="va">None</span> <span class="op">&lt;</span><span class="kw">class</span> <span class="st">&#39;NoneType&#39;</span><span class="op">&gt;</span></span></code></pre></div>
<p>在Python中，如果你不指定return的返回值，则会返回None值。</p>
</div>
<div id="全局变量和局域变量" class="section level3">
<h3><span class="header-section-number">2.12.3</span> 全局变量和局域变量</h3>
<p>全局变量指的是不属于任何函数，但又可以在函数内外访问的变量。而局域变量指的是在函数内部声明的变量，局域变量只能在函数内部使用，无法在函数外访问（函数执行完后，会销毁内部定义的局部变量）。</p>
<p>下面我们通过例子来演示这两者的区别：</p>
<div class="sourceCode" id="cb109"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb109-1"><a href="systax.html#cb109-1"></a>global_var <span class="op">=</span> <span class="dv">12</span>         <span class="co"># 定义全局变量</span></span>
<span id="cb109-2"><a href="systax.html#cb109-2"></a></span>
<span id="cb109-3"><a href="systax.html#cb109-3"></a><span class="kw">def</span> func():</span>
<span id="cb109-4"><a href="systax.html#cb109-4"></a>    local_var <span class="op">=</span> <span class="dv">100</span>     <span class="co"># 定义局部变量</span></span>
<span id="cb109-5"><a href="systax.html#cb109-5"></a>    <span class="bu">print</span>(global_var)   <span class="co"># 可以在函数内部访问全局变量</span></span>
<span id="cb109-6"><a href="systax.html#cb109-6"></a></span>
<span id="cb109-7"><a href="systax.html#cb109-7"></a>func()                  <span class="co"># 调用函数func()</span></span>
<span id="cb109-8"><a href="systax.html#cb109-8"></a></span>
<span id="cb109-9"><a href="systax.html#cb109-9"></a><span class="bu">print</span>(local_var)        <span class="co"># 无法访问变量local_var</span></span></code></pre></div>
<p>上述代码将会出现错误：</p>
<div class="sourceCode" id="cb110"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb110-1"><a href="systax.html#cb110-1"></a><span class="pp">NameError</span>: name <span class="st">&#39;local_var&#39;</span> <span class="kw">is</span> <span class="kw">not</span> defined</span></code></pre></div>
<p>我们再看一个例子：</p>
<div class="sourceCode" id="cb111"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb111-1"><a href="systax.html#cb111-1"></a>xy <span class="op">=</span> <span class="dv">100</span>         <span class="co"># 定义全局变量xy</span></span>
<span id="cb111-2"><a href="systax.html#cb111-2"></a></span>
<span id="cb111-3"><a href="systax.html#cb111-3"></a><span class="kw">def</span> func():</span>
<span id="cb111-4"><a href="systax.html#cb111-4"></a>    xy <span class="op">=</span> <span class="dv">200</span>    <span class="co"># 定义局部变量xy</span></span>
<span id="cb111-5"><a href="systax.html#cb111-5"></a>    <span class="bu">print</span>(xy)   <span class="co"># 此时访问的是局部变量xy</span></span>
<span id="cb111-6"><a href="systax.html#cb111-6"></a></span>
<span id="cb111-7"><a href="systax.html#cb111-7"></a>func()          <span class="co"># 调用函数func()</span></span></code></pre></div>
<p>该代码显示的结果是200，不是100。</p>
<p>使用<code>global</code>关键字，可以将局部变量同全局变量绑定在一起。例如：</p>
<div class="sourceCode" id="cb112"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb112-1"><a href="systax.html#cb112-1"></a>t <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb112-2"><a href="systax.html#cb112-2"></a></span>
<span id="cb112-3"><a href="systax.html#cb112-3"></a><span class="kw">def</span> increment():</span>
<span id="cb112-4"><a href="systax.html#cb112-4"></a>    <span class="kw">global</span> t    <span class="co"># 现在的变量t在函数内外都是一致的</span></span>
<span id="cb112-5"><a href="systax.html#cb112-5"></a>    t <span class="op">=</span> t <span class="op">+</span> <span class="dv">1</span></span>
<span id="cb112-6"><a href="systax.html#cb112-6"></a>    <span class="bu">print</span>(t)    <span class="co"># 输出 2</span></span>
<span id="cb112-7"><a href="systax.html#cb112-7"></a></span>
<span id="cb112-8"><a href="systax.html#cb112-8"></a>increment()</span>
<span id="cb112-9"><a href="systax.html#cb112-9"></a><span class="bu">print</span>(t)        <span class="co"># 输出 2</span></span></code></pre></div>
<p>使用<code>global</code>关键字声明全局变量时，无法直接赋值，比如“global t = 1”的写法存在语法错误。</p>
</div>
<div id="参数的默认值" class="section level3">
<h3><span class="header-section-number">2.12.4</span> 参数的默认值</h3>
<p>为参数指定默认值，只需在定义函数时使用赋值语句即可。例如：</p>
<div class="sourceCode" id="cb113"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb113-1"><a href="systax.html#cb113-1"></a><span class="kw">def</span> func(i, j <span class="op">=</span> <span class="dv">100</span>):</span>
<span id="cb113-2"><a href="systax.html#cb113-2"></a>    <span class="bu">print</span>(i, j)</span></code></pre></div>
<p>上述定义的函数func()有两个参数i和j。j的默认值为100，这意味着我们在调用这个函数的时候可以忽略掉j的值，比如func(2)，运行结果为2 100。</p>
</div>
<div id="关键字参数" class="section level3">
<h3><span class="header-section-number">2.12.5</span> 关键字参数</h3>
<p>为函数传递参数值的方法有两种：位置参数和关键字参数。我们之前调用函数的时候都使用的是位置参数。下面我们看如何使用关键字参数：</p>
<div class="sourceCode" id="cb114"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb114-1"><a href="systax.html#cb114-1"></a><span class="kw">def</span> named_args(name, greeting):</span>
<span id="cb114-2"><a href="systax.html#cb114-2"></a>    <span class="bu">print</span>(greeting <span class="op">+</span> <span class="st">&quot; &quot;</span> <span class="op">+</span> name)</span>
<span id="cb114-3"><a href="systax.html#cb114-3"></a></span>
<span id="cb114-4"><a href="systax.html#cb114-4"></a>named_args(name<span class="op">=</span><span class="st">&#39;jim&#39;</span>, greeting<span class="op">=</span><span class="st">&#39;Hello&#39;</span>)</span>
<span id="cb114-5"><a href="systax.html#cb114-5"></a>named_args(greeting<span class="op">=</span><span class="st">&#39;Hello&#39;</span>, name<span class="op">=</span><span class="st">&#39;jim&#39;</span>)</span>
<span id="cb114-6"><a href="systax.html#cb114-6"></a>named_args(<span class="st">&#39;jim&#39;</span>, greeting<span class="op">=</span><span class="st">&#39;hello&#39;</span>)</span></code></pre></div>
<p>上述代码运行结果都是“hello jim”。</p>
<p>关键字参数使用“name=value”的名称、值对传递数据，正如上面代码演示的那样，使用关键字参数的时候，参数的顺序是可以调换的，而且位置参数和关键字参数可以混合使用（只能先使用位置参数，后使用关键字参数）。</p>
</div>
<div id="返回多个值" class="section level3">
<h3><span class="header-section-number">2.12.6</span> 返回多个值</h3>
<p>我们可以通过在return语句中使用逗号，将多个值返回，这种返回值的类型是元组。例如：</p>
<div class="sourceCode" id="cb115"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb115-1"><a href="systax.html#cb115-1"></a><span class="kw">def</span> bigger(a, b):</span>
<span id="cb115-2"><a href="systax.html#cb115-2"></a>    <span class="cf">if</span> a <span class="op">&gt;</span> b:</span>
<span id="cb115-3"><a href="systax.html#cb115-3"></a>        <span class="cf">return</span> a, b</span>
<span id="cb115-4"><a href="systax.html#cb115-4"></a>    <span class="cf">else</span>:</span>
<span id="cb115-5"><a href="systax.html#cb115-5"></a>        <span class="cf">return</span> b, a</span>
<span id="cb115-6"><a href="systax.html#cb115-6"></a></span>
<span id="cb115-7"><a href="systax.html#cb115-7"></a>s <span class="op">=</span> bigger(<span class="dv">12</span>, <span class="dv">100</span>)</span>
<span id="cb115-8"><a href="systax.html#cb115-8"></a><span class="bu">print</span>(s)</span>
<span id="cb115-9"><a href="systax.html#cb115-9"></a><span class="bu">print</span>(<span class="bu">type</span>(s))</span></code></pre></div>
<p>运行结果为：</p>
<div class="sourceCode" id="cb116"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb116-1"><a href="systax.html#cb116-1"></a>(<span class="dv">100</span>, <span class="dv">12</span>)</span>
<span id="cb116-2"><a href="systax.html#cb116-2"></a><span class="op">&lt;</span><span class="kw">class</span> <span class="st">&#39;tuple&#39;</span><span class="op">&gt;</span></span></code></pre></div>
</div>
<div id="函数文档字符串" class="section level3">
<h3><span class="header-section-number">2.12.7</span> 函数文档字符串</h3>
<p>Python 有一个甚是优美的功能称作文档字符串（Documentation Strings），在称呼它时通常会使用另一个短一些的名字docstrings。DocStrings 是一款你应当使用的重要工具，它能够帮助你更好地记录程序并让其更加易于理解。令人惊叹的是，当程序实际运行时，我们甚至可以通过一个函数来获取文档！</p>
<div class="sourceCode" id="cb117"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb117-1"><a href="systax.html#cb117-1"></a><span class="kw">def</span> print_max(x, y):</span>
<span id="cb117-2"><a href="systax.html#cb117-2"></a>    <span class="co">&#39;&#39;&#39;Prints the maximum of two numbers.</span></span>
<span id="cb117-3"><a href="systax.html#cb117-3"></a></span>
<span id="cb117-4"><a href="systax.html#cb117-4"></a><span class="co">    The two values must be integers.&#39;&#39;&#39;</span></span>
<span id="cb117-5"><a href="systax.html#cb117-5"></a>    <span class="co"># 如果可能，将其转换至整数类型</span></span>
<span id="cb117-6"><a href="systax.html#cb117-6"></a>    x <span class="op">=</span> <span class="bu">int</span>(x)</span>
<span id="cb117-7"><a href="systax.html#cb117-7"></a>    y <span class="op">=</span> <span class="bu">int</span>(y)</span>
<span id="cb117-8"><a href="systax.html#cb117-8"></a></span>
<span id="cb117-9"><a href="systax.html#cb117-9"></a>    <span class="cf">if</span> x <span class="op">&gt;</span> y:</span>
<span id="cb117-10"><a href="systax.html#cb117-10"></a>        <span class="bu">print</span>(x, <span class="st">&#39;is maximum&#39;</span>)</span>
<span id="cb117-11"><a href="systax.html#cb117-11"></a>    <span class="cf">else</span>:</span>
<span id="cb117-12"><a href="systax.html#cb117-12"></a>        <span class="bu">print</span>(y, <span class="st">&#39;is maximum&#39;</span>)</span>
<span id="cb117-13"><a href="systax.html#cb117-13"></a></span>
<span id="cb117-14"><a href="systax.html#cb117-14"></a>print_max(<span class="dv">3</span>, <span class="dv">5</span>)</span>
<span id="cb117-15"><a href="systax.html#cb117-15"></a><span class="bu">print</span>(print_max.__doc__)</span>
<span id="cb117-16"><a href="systax.html#cb117-16"></a>输出：</span>
<span id="cb117-17"><a href="systax.html#cb117-17"></a></span>
<span id="cb117-18"><a href="systax.html#cb117-18"></a>$ python function_docstring.py</span>
<span id="cb117-19"><a href="systax.html#cb117-19"></a><span class="dv">5</span> <span class="kw">is</span> maximum</span>
<span id="cb117-20"><a href="systax.html#cb117-20"></a>Prints the maximum of two numbers.</span>
<span id="cb117-21"><a href="systax.html#cb117-21"></a></span>
<span id="cb117-22"><a href="systax.html#cb117-22"></a>    The two values must be integers.</span></code></pre></div>
<p>该文档字符串所约定的是一串多行字符串，其中第一行以某一大写字母开始，以句号结束。第二行为空行，后跟的第三行开始是任何详细的解释说明。强烈建议你的文档字符串中都遵循这一约定。</p>
<p>我们可以通过使用函数的 <code>__doc__</code>（注意其中的双下划线）属性（属于函数的名称）来获取函数 print_max 的文档字符串属性。</p>
</div>
<div id="lambda表达式" class="section level3">
<h3><span class="header-section-number">2.12.8</span> lambda表达式</h3>
<p>Lambda表达式（或者Lambda形式）用来创建匿名函数。语法如下：</p>
<div class="sourceCode" id="cb118"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb118-1"><a href="systax.html#cb118-1"></a>lambda_expr        ::<span class="op">=</span>  <span class="st">&quot;lambda&quot;</span> [parameter_list]: expression</span></code></pre></div>
<p>个人认为，lambda 是为了减少单行函数的定义而存在的，能够提高代码的简洁性。比如：</p>
<div class="sourceCode" id="cb119"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb119-1"><a href="systax.html#cb119-1"></a>g <span class="op">=</span> <span class="kw">lambda</span> x:x<span class="op">+</span><span class="dv">1</span></span></code></pre></div>
<p>相当于：</p>
<div class="sourceCode" id="cb120"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb120-1"><a href="systax.html#cb120-1"></a><span class="kw">def</span> g(x):</span>
<span id="cb120-2"><a href="systax.html#cb120-2"></a>    <span class="cf">return</span> x<span class="op">+</span><span class="dv">1</span></span></code></pre></div>
</div>
<div id="args-和-kwargs" class="section level3">
<h3><span class="header-section-number">2.12.9</span> <code>*args</code> 和 <code>**kwargs</code></h3>
<p><code>*args</code> 和<code>**kwargs</code>是两个魔法变量。那么它们到底是什么? <code>*args</code> 和 <code>**kwargs</code> 主要用于函数定义。 你可以将不定数量的参数传递给一个函数。</p>
<p>首先，并不是必须写成<code>*args</code> 和<code>**kwargs</code>。 只有变量前面的 <code>*(星号)</code>才是必须的. 你也可以写成<code>*var</code> 和<code>**vars</code>。而写成<code>*args</code> 和<code>**kwargs</code>只是一个通俗的命名约定。</p>
<div id="args-的用法" class="section level4">
<h4><span class="header-section-number">2.12.9.1</span> <code>*args</code> 的用法</h4>
<p><code>*args</code> 是用来发送一个非键值对的可变数量的参数列表给一个函数.</p>
<p>这里有个例子帮你理解这个概念:</p>
<div class="sourceCode" id="cb121"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb121-1"><a href="systax.html#cb121-1"></a><span class="kw">def</span> test_var_args(f_arg, <span class="op">*</span>argv):</span>
<span id="cb121-2"><a href="systax.html#cb121-2"></a>    <span class="bu">print</span>(<span class="st">&quot;first normal arg:&quot;</span>, f_arg)</span>
<span id="cb121-3"><a href="systax.html#cb121-3"></a>    <span class="cf">for</span> arg <span class="kw">in</span> argv:</span>
<span id="cb121-4"><a href="systax.html#cb121-4"></a>        <span class="bu">print</span>(<span class="st">&quot;another arg through *argv:&quot;</span>, arg)</span>
<span id="cb121-5"><a href="systax.html#cb121-5"></a></span>
<span id="cb121-6"><a href="systax.html#cb121-6"></a>test_var_args(<span class="st">&#39;yasoob&#39;</span>, <span class="st">&#39;python&#39;</span>, <span class="st">&#39;eggs&#39;</span>, <span class="st">&#39;test&#39;</span>)</span></code></pre></div>
<p>这会产生如下输出:</p>
<div class="sourceCode" id="cb122"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb122-1"><a href="systax.html#cb122-1"></a><span class="ex">first</span> normal arg: yasoob</span>
<span id="cb122-2"><a href="systax.html#cb122-2"></a><span class="ex">another</span> arg through *argv: python</span>
<span id="cb122-3"><a href="systax.html#cb122-3"></a><span class="ex">another</span> arg through *argv: eggs</span>
<span id="cb122-4"><a href="systax.html#cb122-4"></a><span class="ex">another</span> arg through *argv: test</span></code></pre></div>
</div>
<div id="kwargs-的用法" class="section level4">
<h4><span class="header-section-number">2.12.9.2</span> <code>**kwargs</code> 的用法</h4>
<p><code>**kwargs</code> 允许你将不定长度的键值对, 作为参数传递给一个函数。 如果你想要在一个函数里处理带名字的参数, 你应该使用<code>**kwargs</code>。</p>
<p>这里有个让你上手的例子:</p>
<div class="sourceCode" id="cb123"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb123-1"><a href="systax.html#cb123-1"></a><span class="kw">def</span> greet_me(<span class="op">**</span>kwargs):</span>
<span id="cb123-2"><a href="systax.html#cb123-2"></a>    <span class="cf">for</span> key, value <span class="kw">in</span> kwargs.items():</span>
<span id="cb123-3"><a href="systax.html#cb123-3"></a>        <span class="bu">print</span>(<span class="st">&quot;</span><span class="sc">{0}</span><span class="st"> == </span><span class="sc">{1}</span><span class="st">&quot;</span>.<span class="bu">format</span>(key, value))</span>
<span id="cb123-4"><a href="systax.html#cb123-4"></a></span>
<span id="cb123-5"><a href="systax.html#cb123-5"></a></span>
<span id="cb123-6"><a href="systax.html#cb123-6"></a><span class="op">&gt;&gt;&gt;</span> greet_me(name<span class="op">=</span><span class="st">&quot;yasoob&quot;</span>)</span>
<span id="cb123-7"><a href="systax.html#cb123-7"></a>name <span class="op">==</span> yasoob</span></code></pre></div>
<p>现在你可以看出我们怎样在一个函数里, 处理了一个键值对参数了。</p>
</div>
<div id="使用-args-和-kwargs-来调用函数" class="section level4">
<h4><span class="header-section-number">2.12.9.3</span> 使用 <code>*args</code> 和 <code>**kwargs</code> 来调用函数</h4>
<p>那现在我们将看到怎样使用<code>*args</code>和<code>**kwargs</code> 来调用一个函数。 假设，你有这样一个小函数：</p>
<div class="sourceCode" id="cb124"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb124-1"><a href="systax.html#cb124-1"></a><span class="kw">def</span> test_args_kwargs(arg1, arg2, arg3):</span>
<span id="cb124-2"><a href="systax.html#cb124-2"></a>    <span class="bu">print</span>(<span class="st">&quot;arg1:&quot;</span>, arg1)</span>
<span id="cb124-3"><a href="systax.html#cb124-3"></a>    <span class="bu">print</span>(<span class="st">&quot;arg2:&quot;</span>, arg2)</span>
<span id="cb124-4"><a href="systax.html#cb124-4"></a>    <span class="bu">print</span>(<span class="st">&quot;arg3:&quot;</span>, arg3)</span></code></pre></div>
<p>你可以使用<code>*args</code>或<code>**kwargs</code>来给这个小函数传递参数。 下面是怎样做：</p>
<div class="sourceCode" id="cb125"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb125-1"><a href="systax.html#cb125-1"></a><span class="co"># 首先使用 *args</span></span>
<span id="cb125-2"><a href="systax.html#cb125-2"></a><span class="op">&gt;&gt;&gt;</span> args <span class="op">=</span> (<span class="st">&quot;two&quot;</span>, <span class="dv">3</span>, <span class="dv">5</span>)</span>
<span id="cb125-3"><a href="systax.html#cb125-3"></a><span class="op">&gt;&gt;&gt;</span> test_args_kwargs(<span class="op">*</span>args)</span>
<span id="cb125-4"><a href="systax.html#cb125-4"></a>arg1: two</span>
<span id="cb125-5"><a href="systax.html#cb125-5"></a>arg2: <span class="dv">3</span></span>
<span id="cb125-6"><a href="systax.html#cb125-6"></a>arg3: <span class="dv">5</span></span>
<span id="cb125-7"><a href="systax.html#cb125-7"></a></span>
<span id="cb125-8"><a href="systax.html#cb125-8"></a><span class="co"># 现在使用 **kwargs:</span></span>
<span id="cb125-9"><a href="systax.html#cb125-9"></a><span class="op">&gt;&gt;&gt;</span> kwargs <span class="op">=</span> {<span class="st">&quot;arg3&quot;</span>: <span class="dv">3</span>, <span class="st">&quot;arg2&quot;</span>: <span class="st">&quot;two&quot;</span>, <span class="st">&quot;arg1&quot;</span>: <span class="dv">5</span>}</span>
<span id="cb125-10"><a href="systax.html#cb125-10"></a><span class="op">&gt;&gt;&gt;</span> test_args_kwargs(<span class="op">**</span>kwargs)</span>
<span id="cb125-11"><a href="systax.html#cb125-11"></a>arg1: <span class="dv">5</span></span>
<span id="cb125-12"><a href="systax.html#cb125-12"></a>arg2: two</span>
<span id="cb125-13"><a href="systax.html#cb125-13"></a>arg3: <span class="dv">3</span></span></code></pre></div>
</div>
<div id="标准参数与argskwargs在使用时的顺序" class="section level4">
<h4><span class="header-section-number">2.12.9.4</span> 标准参数与<code>*args</code>、<code>**kwargs</code>在使用时的顺序</h4>
<p>如果你想在函数里同时使用所有这三种参数， 顺序是这样的：</p>
<div class="sourceCode" id="cb126"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb126-1"><a href="systax.html#cb126-1"></a>some_func(fargs, <span class="op">*</span>args, <span class="op">**</span>kwargs)</span></code></pre></div>
</div>
</div>
<div id="参考资料" class="section level3">
<h3><span class="header-section-number">2.12.10</span> 参考资料</h3>
<p><a href="https://docs.pythontab.com/interpy/">Python进阶</a></p>

</div>
</div>
</div>
<div class="footnotes">
<hr />
<ol start="1">
<li id="fn1"><p>美国信息交换标准码 (American Standard Code for Information Interchange) 是由美国国家标准学会 (American National Standard Institute,ANSI) 制定的单字节字符编码方案，供不同计算机在相互通信时用作共同遵守的西文字符编码标准，它已被国际标准化组织（ISO）定为国际标准，称为 ISO646 标准。比较字符。<a href="systax.html#fnref1" class="footnote-back">↩︎</a></p></li>
</ol>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="ide.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="oop.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/yangjh-xbmu/learningpython/edit/master/0201-syntax-comment.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["learningpython.pdf", "learningpython.epub"],
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
